【问题标题】:DisplayMemberPath on ComboBox not working but Binding seems correct, can't get the DisplayMemberPath to displayComboBox 上的 DisplayMemberPath 不起作用但绑定似乎正确,无法显示 DisplayMemberPath
【发布时间】:2018-02-14 17:35:48
【问题描述】:

我今天在这个问题上花了很长时间,所以我将它作为一个问题发布,然后发布答案,以便 可以避免与我为过去 183 分钟。

这是我的源代码的简单版本(也许你的看起来像这样)

.xaml(视图):

<ComboBox SelectedItem="{Binding WindDirection}" ItemsSource="{Binding WindDirections}" DisplayMemberPath="DisplayText" IsEditable="False"/>

.cs(视图模型):

public class WindDirectionViewModel{
   //I realize that there may be problems in this code, it's not my real code, just a quick sample

   ...code

   List<WindDirectionObject> WindDirections = new List<WindDirectionObject>();
   WindDirectionObject WindDirection = new WindDirectionObject(); 

   ...code

   public string DisplayText = WindDirections.First(x => x.Equals(WindDirection)).DisplayString;

   ...code

}

所有代码都完美工作,并且相同的 (.cs) ViewModel 甚至可以在另一个 (.xaml) 视图中正确显示,但在 this 视图中它不是t 正常工作。确切的问题是第一次打开视图时 ComboBox 中没有文本,即使 DisplayText 有一个值!断点显示 DisplayText 值正在正确计算以及所有内容,但在第一次打开视图时不会显示该值。

【问题讨论】:

    标签: combobox wcf-binding generic.xaml


    【解决方案1】:

    如果您在 .xaml 组合框中使用 DisplayMemberPath 属性 - 您必须ItemsSource 属性放在之前.xaml... 中的 SelectedItem 属性或 DisplayMemberPath 值不显示。

    之前:

    <ComboBox ItemsSource="{Binding WindDirections}" SelectedItem="{Binding WindDirection}" DisplayMemberPath="DisplayText" IsEditable="False"/>
    

    之后:

    <ComboBox SelectedItem="{Binding WindDirection}" ItemsSource="{Binding WindDirections}" DisplayMemberPath="DisplayText" IsEditable="False"/>
    

    巴姆。完美运行。这可能无法解决您的问题,但它肯定解决了我的问题……希望这能加快您的开发时间。 :)

    【讨论】:

      猜你喜欢
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多