Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Castom组合框和丢失绑定_C#_Mvvm - Fatal编程技术网

C# Castom组合框和丢失绑定

C# Castom组合框和丢失绑定,c#,mvvm,C#,Mvvm,我正试图编写自己的组合框,但遇到了一个问题 public class TestComboBox : ComboBox { public static readonly DependencyProperty SelectedCarProperty = DependencyProperty.Register("SelectedCar", typeof(string), typeof(TestComboBox), new FrameworkPropertyMetadata(null, OnSe

我正试图编写自己的组合框,但遇到了一个问题

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>() { "Select Car", "BMW", "Mersedes", "Audi" };

        this.ItemsSource = this.Cars;
        this.SelectedItem = this.Cars[0];
    }

    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;
    }
}
如何设置默认值而不丢失与VM的绑定

附言

更新问题:

<propgrid:PropertyGrid Margin="0,0,11,7">
    <propgrid:PropertyGrid.Items>
        <propgrid:PropertyGridCategoryItem DisplayName="Main">
                <propgrid:PropertyGridPropertyItem Value="{Binding Car, Mode=TwoWay}"
                                               DisplayName="Car">
                <propgrid:PropertyGridPropertyItem.ValueTemplate>
                        <DataTemplate>
                            <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>

尝试为绑定提供Mode=TwoWay

更新答案:

双向引用您的代码集模式

Value="{Binding Car}" 
也是


Car绑定到Value,Value绑定到SelectedCar

我使用此绑定更新了问题。我试着用双向绑定,但没用
Value="{Binding Car}"