【问题标题】:ComboBox SelectedItem BindingComboBox SelectedItem 绑定
【发布时间】:2019-11-10 11:22:53
【问题描述】:

我的视图中有一个组合框:

<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));
    }
}

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

如何填充组合框:

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 绑定到选定的 Firma?

很难使用 MVVM,但我喜欢它。

【问题讨论】:

    标签: c# wpf xaml mvvm observablecollection


    【解决方案1】:

    您应该将ComboBoxDataContext 设置为您的视图模型的实例。否则绑定将不起作用:

    this.comboBox1.DataContext = MAList;
    

    另请注意,您的资源的 _MandantList 支持字段不应公开。事实上,你根本不需要它:

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

    设置DataContext 应该会在您选择ComboBox 中的项目时设置CurrentMandant 属性。但它不会设置IsChecked 属性。

    【讨论】:

    • 你是个天才!!!搜索了几个小时......一个简单的this.comboBox1.DataContext = MAList; 解决了我的问题。也感谢支持领域的帮助!
    • @DerKiLLa:注意你一般设置整个窗口的DataContext,让控件,比如本例中的ComboBox,继承它。
    • 好吧...我还用&lt;TextBlock x:Name="website" Text="{Binding CurrentMandant.Homepage}"/&gt;绑定了一个TextBlock到最后真的很简单:D
    猜你喜欢
    • 2011-11-01
    • 2015-06-28
    • 2017-04-30
    • 2023-04-03
    • 2010-10-24
    • 1970-01-01
    • 2014-10-10
    • 2013-11-07
    • 2017-11-09
    相关资源
    最近更新 更多