Hello, I would like to use my implementation of ComboBox together with PropertyGrid. The problem is that I can not make Binding to viewmodel.
public class TestComboBox : ComboBox
{
public static readonly DependencyProperty SelectedCarProperty = DependencyProperty.Register("SelectedCar", typeof(string), typeof(TestComboBox), new FrameworkPropertyMetadata(null, OnSelectedCarPropertyChanged));
public TestComboBox()
{
this.Cars = new ObservableCollection<string>() { "BMW", "Mersedes", "Audi" };
this.ItemsSource = this.Cars;
this.SelectedCar = "Mersedes";
}
public string SelectedCar
{
get { return (string)GetValue(SelectedCarProperty); }
set { this.SetValue(SelectedCarProperty, value); }
}
public IList<string> Cars { get; private set; }
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
base.OnSelectionChanged(e);
this.SelectedCar = (string)SelectedItem;
}
private static void OnSelectedCarPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
var car = (string)e.NewValue;
var editor = (TestComboBox)source;
editor.SelectedItem = car;
}
}
This code just does not work. How can I make a binding to viewmodel?
<StackPanel>
<Button Content="Watch"
Command="{Binding Watch}"/>
<propgrid:PropertyGrid Margin="0,0,11,7">
<propgrid:PropertyGrid.Items>
<propgrid:PropertyGridCategoryItem DisplayName="Main">
<propgrid:PropertyGridPropertyItem Value="{Binding Car}"
DisplayName="Car">
<propgrid:PropertyGridPropertyItem.ValueTemplate>
<DataTemplate>
<!--<TextBlock Text="{Binding Value, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}}"/>-->
<wpfApplication1:TestComboBox SelectedCar="{Binding Value, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}}" />
</DataTemplate>
</propgrid:PropertyGridPropertyItem.ValueTemplate>
</propgrid:PropertyGridPropertyItem>
</propgrid:PropertyGridCategoryItem>
</propgrid:PropertyGrid.Items>
</propgrid:PropertyGrid>
</StackPanel>