【问题标题】:Binding ComboBox selected Item to a property of the View Model将 ComboBox 选定项绑定到视图模型的属性
【发布时间】:2013-04-28 21:19:11
【问题描述】:

您好,我正在尝试将我的 ComboBox 选定项绑定到我的视图模型中的一个属性,其中 setter 将获取此值并执行一些其他逻辑。现在我的 ComboBox 工作正常,可以从 observablecollection 系统中提取项目,但是我无法将作为序列的 selectedItem 绑定到属性。所选项目未获取 ComboBox 的字符串值。其他一切都可以,在后面的代码中将 DataContext 分配给视图。任何想法都是我的 viewModel:

public class CablingRequests : ObservableCollection<CablingRequest>
{
    public ObservableCollection<CablingRequest> PendingRequests { get; set; }
    public ObservableCollection<CablingRequest> ProcessedRequests { get; set; }
    public ObservableCollection<CablingRequest> Systems { get; set; }
    public ObservableCollection<CablingRequest> SelectedSystemConfiguration { get; set; }

    private string _serial;
    public string Serial
    {
        get { return _serial; }
        set
        {
            if (_serial == value)
                return;
            _serial = value;
            GetSelectedSystemConfiguration(_serial);
        }
    }

还有我的组合框 xaml 代码:

<ComboBox x:Name="ComboBoxSerial" ItemsSource="{Binding Path=Systems}"
 DisplayMemberPath="SerialNumber" SelectedValue="{Binding Path=Serial, Mode=TwoWay}"
 IsSynchronizedWithCurrentItem="True" MinWidth="150" />

【问题讨论】:

  • 我的 xaml 在这里丢失:

标签: mvvm combobox bind


【解决方案1】:

您的组合框绑定到CablingRequest 的集合,因此您应该将SelectedItem 绑定到CablingRequest 的实例,或者如果您只需要序列号,那么您应该将SelectedValuePath 设置为CablingRequest 类型的“SerialNumber”属性。

更多信息请参见Difference between SelectedItem, SelectedValue and SelectedValuePath

【讨论】:

  • 该链接非常有帮助,并帮助我解决了让带有字符串属性绑定的 XML 填充组合框按预期工作的问题。
猜你喜欢
  • 1970-01-01
  • 2010-12-02
  • 2023-03-20
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多