C#“如果”绑定值 [英] C# 'if' Binding value

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

问题描述

我有一个由绑定填充,在一个名为类列表视图。



这里是我的代码示例:

 < DataTemplate的数据类型=房子> 
< TextBlock的文本={结合sold_status}/>
< / DataTemplate中>



正如你所看到的,我的变量名是 sold_status 。这是一个布尔



我要显示为<$ C要么卖还是不出售 $ C> 1 和 0 分别。



能否在时尚的?基于价值的语句



所以才让你可以想像我想达到什么:

 <数据类型的DataTemplate =房子> 
< TextBlock的文本=({结合sold_status} == 1)'卖'其他'不卖'/>
< / DataTemplate中>


解决方案

您需要创建与DataTriggers样式根据需要设置的属性。你也可以使用一个转换器,但改变基于基础数据的UI控件的属性到底是什么触发/风格是什么。



..其实,我可以看到你'重新基本上'sold_status转换'到一些文字。为此,使用转换器。我会后一个简单的例子。



看到这里最多的回答:的 WPF:显示一个布尔值" YES接/"否]。 - 它有一个例子转换器类,你可以重新调整


I've got a list view that is populated by a Binding, on a class named House.

Here's an example of my code:

<DataTemplate DataType="house">
    <TextBlock Text="{Binding sold_status}" />
</DataTemplate>

As you can see, one of my variable names is sold_status. This is a bool.

I want to show either "SOLD" or "NOT SOLD" for 1 and 0 respectively.

Is it possible to fashion an if statement based on the value?

So just so that you can visualise what I want to achieve:

<DataTemplate DataType="house">
    <TextBlock Text="({Binding sold_status} == 1) 'SOLD' else 'NOT SOLD'" />
</DataTemplate>

解决方案

You'll want to create a Style with DataTriggers in to set the properties as needed. You could also use a converter, but changing UI control properties based on underlying data is exactly what triggers/styles are all about.

..In fact, I can see you're basically 'converting' sold_status to a bit of text. For that, use a converter. I'll post a quick example..

See the top answer here: WPF: Display a bool value as "Yes" / "No" - it has an example converter class you could repurpose.

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

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