将常量Collection绑定到ComboBox& SelectedItem到TextBox

时间:2018-04-24 21:49:11

标签: c# wpf mvvm data-binding combobox

我是WPF中MVVM的新手,我遇到了以下问题。

我试图拥有的是两个ComboBox,每个都绑定到与ObservableCollection<TwoProperties> DList相同的ItemsSource属性并且使用synchronized SelectedItem,所以我在我的XAML中写了这个

<ComboBox ItemsSource="{Binding DList}" DisplayMemberPath="Property1" SelectedItem="{Binding SelectedD}" />
<ComboBox ItemsSource="{Binding DList}" DisplayMemberPath="Property2" SelectedItem="{Binding SelectedD}" />

使用此viewmodel

public class ViewModel : INotifyPropertyChanged
{
    private ObservableCollection<TwoProperties> _dList =
        new ObservableCollection<TwoProperties> {
             new TwoProperties(1,"one"),
             new TwoProperties(2,"two")
        };

    public ObservableCollection<TwoProperties> DList
    {
        get { return _dList; }
        set { _dList = value; OnPropertyChanged("DList"); }
    }

    private TwoProperties _selectedD;
    public TwoProperties SelectedD
    {
        get { return _selectedD; }
        set { _selectedD = value; OnPropertyChanged("SelectedD"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

,其中

public class TwoProperties
{
    public double Property1 { get; set; }
    public string Property2 { get; set; }

    public TwoProperties (double p1, string p2)
    {
        Property1 = p1;
        Property2 = p2;
    }
}

我还想有两个TextBox,它们显示同步ComboBoxes当前SelectedItem的属性。 Property1的{​​{1}}和Property2属性应可编辑,但SelectedD 应保持不变/只读,不会更改其值

ObservableCollection<TwoProperties> _dList

当我编辑TextBoxes 并因此编辑SelectedD时, _dList也会更改其值,这不是我想要的。

我希望我能解释一下我的问题。我确定我在这里错过了一些简单的东西。

1 个答案:

答案 0 :(得分:0)

这可以通过将TextBoxes的绑定模式更改为以下一种方式轻松实现:

    <TextBox Text="{Binding SelectedD.Property1,Mode=OneWay}" />
    <TextBox Text="{Binding SelectedD.Property2,Mode=OneWay}" />

因此,当您更改textBox值时,更改不会反映回Observable集合对象。

请注意,您可以通过修改方法来删除视图模型OnPropertyChanged中的魔法字符串:

private void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

然后你可以在视图模型中的任何属性的setter中调用它,而不传递属性的名称,如下所示:

    private TwoProperties _selectedD;
    public TwoProperties SelectedD
    {
        get { return _selectedD; }
        set { _selectedD = value; OnPropertyChanged(); }
    }

编辑2: 更新我的绑定和视图模型以获取视图模型中的已编辑值

查看模型更新:

private double? editPropertyOne;
    public double? EditPropertyOne
    {
        get { return editPropertyOne; }
        set
        {
            editPropertyOne = value;
            OnPropertyChanged();
        }
    }

    private string editPropertyTwo;
    public string EditPropertyTwo
    {
        get { return editPropertyTwo; }
        set
        {
            editPropertyTwo = value;
            OnPropertyChanged();
        }
    }

    private TwoProperties _selectedD;
    public TwoProperties SelectedD
    {
        get { return _selectedD; }
        set
        {
            _selectedD = value; OnPropertyChanged();
            if (_selectedD != null)
            {
                EditPropertyOne = _selectedD.Property1;
                EditPropertyTwo = _selectedD.Property2;
            }
        }
    }

Xaml更改:

    <TextBox Text="{Binding EditPropertyOne}" />
    <TextBox Text="{Binding EditPropertyTwo}" />