How to use my implementation of ComboBox together with PropertyGrid.

Grids for WPF Forum

Posted 8 years ago by Alex4
Version: 14.2.0611

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)
            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?

        <Button Content="Watch"
                Command="{Binding Watch}"/>
        <propgrid:PropertyGrid Margin="0,0,11,7">
            <propgrid:PropertyGridCategoryItem DisplayName="Main">
                <propgrid:PropertyGridPropertyItem Value="{Binding Car}"
                            <!--<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}}}" />


Comments (1)

Answer - Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA


I don't see anything wrong offhand with that.  Does it work with a regular TextBox if you uncomment your TextBlock code instead and switch it to a TextBox?  If it works in that scenario, then it's probably something your custom ComboBox implementation that needs tweaking.

Actipro Software Support

The latest build of this product (v22.1.3) was released 26 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.