【问题标题】:WPF. Help to bind combobox itemsWPF。帮助绑定组合框项目
【发布时间】:2015-07-13 09:33:11
【问题描述】:

我有一个视图模型,它有一个列表(不是可观察的集合,我想这并不重要)可供选择用于文档(库存、装箱单等)和一个 文档 分配了确切的类实例

        public class MyViewModel
        {
                public DocumentViewModel Document { get; set; }
                public IList<ClassViewModel> Classes { get; set; }
        }

       public class DocumentViewModel
       {
            public ClassViewModel Class { get; set; }
            ...
       }

    public class ClassViewModel
    {
        public string Title { get; set; }
        ...
    }

我必须显示 combobox 应该显示可用的文档类,并且选定的项目应该是 MyViewModel 实例的文档类。

以下代码对我不起作用:

 <ComboBox 
     ItemsSource="{Binding Path=Classes}" 
     DisplayMemberPath="Title" 
     SelectedValuePath="Title"
     SelectedValue="{Binding Path=Document.Class, Mode=TwoWay}"
/>

我在这里做错了什么?

【问题讨论】:

  • 代码看起来不错,你能解释一下“以下代码对我不起作用:”到底是什么意思吗?什么都没有显示?该值未设置?究竟是什么不工作?
  • 显示组合框项目,但未从项目中选择 SelectedValue,更改时所选值不同步
  • 附注此外,当我更改组合框中的项目时,组合框由于某种原因被红色边框包围。
  • 红框通常是因为验证规则。尝试给 ComboBox 一个 x:Name 然后将 SelectedValue 绑定到 TextBlock &lt;ComboBox x:Name="ComboBox" ItemsSource="{Binding Path=Classes}" DisplayMemberPath="Title" SelectedValuePath="Title" SelectedValue="{Binding Path=Document.Class, Mode=TwoWay}" /&gt; &lt;TextBlock Text="{Binding ElementName=ComboBox, Path=SelectedValue}"/&gt; 你在 TextBlock 中得到了什么吗??

标签: wpf combobox


【解决方案1】:

最后,我通过删除更改了我的标记

SelectedValuePath="Title"

现在一切正常!

【讨论】:

    猜你喜欢
    • 2022-12-06
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2012-06-18
    • 2011-03-09
    相关资源
    最近更新 更多