更改绑定,如果值是空的 [英] Change binding if value is empty

查看:130
本文介绍了更改绑定,如果值是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变绑定如果值为null或空。

I want to change binding if the value is null or empty.

我做这个为例来解释:

List<test> list = new List<test>();
list.Add(new test { Name1 = "Bill", Name2 = "Jack" });
list.Add(new test { Name1 = "", Name2 = "Adam" });
TestDataGrid.ItemsSource = list;

XAML

<DataGridTextColumn Header="Name" Binding="{Binding Name1}" />

在这种情况下会显示:

in this case will show :

Bill

""

我想如果第一个名称为null或空将显示名称2 ,然后

Bill

Adam

在另一种方式我想做的事:

in another way I want to do :

<DataGridTextColumn Header="Name" Binding="if({Binding Name1} == null)
                                  {Binding Name2} else {Binding Name1}   " />

修改

我东西转换器解决,但我不能发送名称2 来改变值,如果名1 为空

I thing that Converter resolve that, but I can't send the Name2 to change the value if the Name1 is null

推荐答案

我想创建一个新的属性:

I'd create a new property:

public string Name { get { return string.IsNullOrEmpty(Name1) ? Name2 : Name1; } }

<DataGridTextColumn Header="Name" Binding="{Binding Name}" />

这篇关于更改绑定,如果值是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆