简体   繁体   English

ComboBox SelectedItem 绑定

[英]ComboBox SelectedItem Binding

I have a ComboBox in my View :我的视图中有一个 ComboBox :

<ComboBox Name="comboBox1" ItemsSource="{Binding MandantList}" SelectedItem="{Binding CurrentMandant, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Firma}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Here is my Model :这是我的模型

public class MandantListItem : INotifyPropertyChanged
{
    public MandantListItem() { }

    string _Firma;
    bool _IsChecked;

    public string Firma
    {
        get { return _Firma; }
        set { _Firma = value; }
    }
    public bool IsChecked
    {
        get
        {
            return _IsChecked;
        }
        set
        {
            _IsChecked = value;
            OnPropertyChanged(nameof(IsChecked));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

And here is my ViewModel :这是我的ViewModel

public class MaViewModel : INotifyPropertyChanged
{
    public ObservableCollection<MandantListItem> MandantList { get { return _MandantList; } }
    public ObservableCollection<MandantListItem> _MandantList = new ObservableCollection<MandantListItem>();

    private MandantListItem _CurrentMandant;
    public MandantListItem CurrentMandant
    {
        get { return _CurrentMandant; }
        set
        {
            if (value != _CurrentMandant)
            {
                _CurrentMandant = value;
                OnPropertyChanged("CurrentMandant");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

How to fill the ComboBox:如何填充组合框:

public zTiredV2.ViewModel.MaViewModel MAList = new zTiredV2.ViewModel.MaViewModel();
this.comboBox1.ItemsSource = MAList.MandantList;
MAList.MandantList.Add(new zTiredV2.Model.MandantListItem { Firma = "A", Homepage = "a.com", IsChecked = false });
MAList.MandantList.Add(new zTiredV2.Model.MandantListItem { Firma = "B", Homepage = "b.com", IsChecked = false });

But my item doesnt update ... tried also via IsChecked, but no success either ... when i iterate through MAList, IsChecked is always false.但是我的项目没有更新……也通过 IsChecked 尝试过,但也没有成功……当我遍历 MAList 时,IsChecked 始终为 false。 And how can i bind a TextBlock to the selected Firma?以及如何将 TextBlock 绑定到选定的 Firma?

Have a hard time with MVVM, but i like it.使用 MVVM 很难,但我喜欢它。

You should set the DataContext of the ComboBox to an instance of your view model.您应该将ComboBoxDataContext设置为您的视图模型的实例。 Otherwise the bindings won't work:否则绑定将不起作用:

this.comboBox1.DataContext = MAList;

Also note that the _MandantList backing field for your property shouldn't be public.另请注意,您的资产的_MandantList支持字段不应公开。 In fact, you don't need it at all:事实上,你根本不需要它:

public ObservableCollection<MandantListItem> MandantList { get; } = new ObservableCollection<MandantListItem>();

Setting the DataContext should cause the CurrentMandant property to get set when you select an item in the ComboBox .当您在ComboBox选择一个项目时,设置DataContext应该会导致设置CurrentMandant属性。 It won't set the IsChecked property though.但是它不会设置IsChecked属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM