【WPF绑定2】 ComboBox SelectedValue复杂数据类型绑定

前言

这次绑定是一次非常痛苦的经历,因为SelectedValue总是不能生效!我一度怀疑是wpf的Bug。其实还是自己没搞清楚。

在之前的一篇文章中:

http://t.csdn.cn/A4W6Ahttp://t.csdn.cn/A4W6A我也写个ComboBox的绑定,但是当时没有指定ItemsSource,而是将子项直接写道前台里面。

因为当时每个条目就是简单的字符串而且是固定的。而这次每个Items都是一个复杂的数据结构,而且ItemsSource会发生变化。

解决方案

想让SelectedValue绑定生效,关键三个属性:

  • ItemsSource
  • SelectedValue (也可以使用SelectedItem进行替换!)
  • DisplayMemberPath

前台代码:

<ComboBox hc:TitleElement.Title="选择输入"                      
             ItemsSource="{Binding ListInput}"                                    
             SelectedValue="{Binding proprecessImage.CurSelectInput, Mode=TwoWay}"
             DisplayMemberPath="DisplayName">
</ComboBox>

ItemsSource,就是我们数据源,它决定了ComboBox 有多少个Items。

ItemsSource的数据源也就是变量ListInput:

public ObservableCollection<ToolsNodeItem> ListInput { get; set; } = new ObservableCollection<ToolsNodeItem>();

ListInput中的子项的类型是ToolsNodeItem:

    public class ToolsNodeItem:BindableBase
    {
        public string Icon { get; set; }
        public string EditIcon { get; set; }

        private string dsplayName="";

        public string DisplayName
        {
            get { return dsplayName; }
            set { SetProperty(ref dsplayName, value); }
        }

        public string TypeName { get; set; }
    }

DisplayMemberPath,决定你的每个Item中在界面上显示的内容!因为此时Item是个复杂类型(ToolsNodeItem),所以界面是不知道你想显示啥的。如果不指定DisplayMemberPath你会看到这样的结果:

 因为,不知道显示啥,所以显示了Item的类型,相当于是将Item进行了toString()。

所以我们通过DisplayMemberPath告诉wpf我们想显示ToolsNodeItem中的一个属性,这我选择的是DisplayName。注意,DisplayMemberPath直接赋值类型的字符串名称,不需要bingding表达式

最后是今天的主角,SelectedValue,首先我们声明一个ToolsNodeItem类型的变量:

/// <summary>
/// 当前选择的输入
/// </summary>
private ToolsNodeItem curSelectInput = new ToolsNodeItem();
public ToolsNodeItem CurSelectInput
{
    get { return curSelectInput; }
    set { SetProperty(ref curSelectInput, value); }
}

然后通过通过bingding表达式绑定即可!

SelectedValue="{Binding proprecessImage.CurSelectInput, Mode=TwoWay}"

小结

1 以上通知形式写法基于prism。可以改为其他的通知形式。

2 DisplayMemberPath 和SelectValuePath 是异同点的。

不同点是:DisplayMemberPath 是界面显示相关的。SelectValuePath是和最终Item获取到的值相关的。

相同点是:都是对复杂的结构的Item的类型进行筛选。

我这里因为需要ToolsNodeItem的完整值,就不需要SelectValuePath进行筛选,

但是又需要对界面显示筛选,所以这里就用到了DisplayMemberPath

posted @ 2022-12-20 16:20  宋桓公  阅读(341)  评论(0编辑  收藏  举报