c# - ComboBox SelectedItem 绑定(bind)

标签 c# wpf xaml mvvm observablecollection

我的 中有一个 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>

这是我的型号 :
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));
    }
}

这是我的查看型号 :
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));
    }
}

如何填充组合框:
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 });

但是我的项目没有更新......也通过 IsChecked 尝试过,但也没有成功......当我遍历 MAList 时,IsChecked 总是错误的。以及如何将 TextBlock 绑定(bind)到选定的 Firma?

MVVM 很难,但我喜欢它。

最佳答案

您应该设置 DataContextComboBox到您的 View 模型的实例。否则绑定(bind)将不起作用:

this.comboBox1.DataContext = MAList;

另请注意 _MandantList您的属性(property)的支持字段不应该是公开的。事实上,你根本不需要它:
public ObservableCollection<MandantListItem> MandantList { get; } = new ObservableCollection<MandantListItem>();

设置DataContext应该会导致 CurrentMandant当您在 ComboBox 中选择一个项目时要设置的属性.它不会设置 IsChecked属性(property)虽然。

关于c# - ComboBox SelectedItem 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56807954/

相关文章:

c# - 设置 InkCanvas.InkPresenter 绑定(bind)

c# - FxCop 说我应该返回一个通用列表接口(interface)而不是字节数组。我是不是该?

c# - MVVM 将嵌套 subview 连接到 subview 模型

wpf - RowDefinition 高度 ="10*"在 XAML 网格中意味着什么?

c# - WPF 使用 x :Name as a key 绑定(bind)到字典

c# - 如何在不使用外部库的情况下在 Windows Phone 8 或 8.1 中绘制图表?

C# 关于 IEnumerable<T>.Aggregate

c# - Ninject + 自动发现

wpf - 调试 ElementName 绑定(bind)

XAML 水平滚动有两个项目