How to use my implementation of ComboBox together with PropertyGrid.

Grids for WPF Forum

Posted 9 years ago by Alex4
Version: 14.2.0611
Avatar

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>

 TestApp

Comments (1)

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

Hello,

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 (v24.1.2) was released 1 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.