【问题标题】:Cannot convert 'System.Windows.Controls.ListBoxItem: MyType' from type 'ListBoxItem' to type 'MyType'无法将“System.Windows.Controls.ListBoxItem: MyType”从“ListBoxItem”类型转换为“MyType”类型
【发布时间】:2011-02-02 13:58:22
【问题描述】:

我有以下 XAML:

<ListBox SelectedItem="{Binding SelectedTeam}">
    <ListBoxItem Content="{Binding Match.HomeTeam}" />
    <ListBoxItem Content="{Binding Match.RoadTeam}" />
</ListBox>

Match 的两支球队出现在列表框中。但是,当我单击其中一项以设置视图模型的 SelectedTeam 属性时,我在 Visual Studio 的输出窗口中有此消息:

System.Windows.Data 错误:23:不能 兑换 'System.Windows.Controls.ListBoxItem: Emidee.CommonEntities.Team' 来自类型 'ListBoxItem' 键入 'Emidee.CommonEntities.Team' 用于 'en-US' 文化默认 转换;考虑使用转换器 绑定的属性。 NotSupportedException:'System.NotSupportedException: TypeConverter 无法从 System.Windows.Controls.ListBoxItem。

解决此问题的一种方法是在我的视图模型上创建一个 IEnumerable,它将返回 Match.HomeTeam 和 Match.RoadTeam,并将此属性绑定到列表框的 ItemsSource 属性。

但是有没有其他解决方案,可以让我像以前一样在 XAML 中指定项目?

提前致谢

迈克

【问题讨论】:

    标签: wpf data-binding binding


    【解决方案1】:

    将 SelectedValue 与 SelectedValuePath 结合使用:

    <ListBox SelectedValue="{Binding SelectedTeam}"
             SelectedValuePath="Content">
        <ListBoxItem Content="{Binding Match.HomeTeam}" />
        <ListBoxItem Content="{Binding Match.RoadTeam}" />
    </ListBox>
    

    【讨论】:

      猜你喜欢
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 2015-11-09
      • 2013-06-11
      • 2021-08-05
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多