访问 WPF 中 TextBlock 的值绑定 TextProperty
我有两个 TextBlock,其 TextProperty 绑定到类中的两个属性。即 StackingText 和 StackingRate... 我希望如果 StackingRate 为 null 或空,则 StackingText 应设置为 null 或空,因为 StackingText 的默认值是“收费”。
<TextBlock Name="stackingText"
Margin="270,215,0,0"
Text="{Binding Path=StackingText}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontSize="14"
Padding="5"/>
<TextBlock Name="stackingRate"
Margin="270,215,0,0"
Text="{Binding Path=StackingRate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontSize="14"
Padding="5"/>
实际上我正在打印此页面,因此当 stackingRate 为空时, stackingText 具有值是没有意义的。
I have two TextBlock whose TextProperty is binded to two properties in class. Namely, StackingText and StackingRate...
I want that if StackingRate is null or empty, the StackingText should be set to null or empty since the default value for StackingText is "Is charged at".
<TextBlock Name="stackingText"
Margin="270,215,0,0"
Text="{Binding Path=StackingText}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontSize="14"
Padding="5"/>
<TextBlock Name="stackingRate"
Margin="270,215,0,0"
Text="{Binding Path=StackingRate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontSize="14"
Padding="5"/>
Actually i am printing this page, so it doesn't make sense that when stackingRate is empty, stackingText has value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将
DataTrigger
用于 stackingTextTextBlock
,在其中绑定到 stackingRate `TextBlock' 的文本,如果值为“”(null 或空),则设置也发短信给“”。由于触发器的工作方式,这也将使属性 StackingText 保持不变You could use a
DataTrigger
for the stackingTextTextBlock
where you bind to Text for the stackingRate `TextBlock' and if the Value is "" (null or empty) then you set the Text to "" as well. This will also leave the property StackingText untouched because of the way that Triggers work正如 Melak 指出的那样,您可以通过触发器来完成此操作。不过,如果您使用 MVVM,您甚至不需要问这个问题。
我使用 WPF 的次数越多,我就越清楚,如果您只实现一个视图模型,一切都会变得更简单。 写一些东西来支持一个观点,这有点乏味
是的,总是需要一遍又一遍地 。 (如果 C# 有一个自动实现视图模型 getter 和 setter 的语法结构,那就太好了。)但是一旦有了它,如何修改它以注入逻辑就很明显了:
You can accomplish this in a trigger, as Meleak points out. If you were using MVVM, though, you wouldn't even have needed to ask this question.
The more I work with WPF, the clearer it is to me that everything's simpler if you just implement a view model. Yes, it's a little tedious to always have to write the likes of
over and over just to support a view. (It'd be nice if C# had a little syntax construct that implemented view model getters and setters automatically.) But once you have that, it's obvious how to modify it to inject logic:
一个简单的可重用解决方案是编写一个转换器,将默认字符串作为参数。当您设置费率时,此转换器可以决定仅显示文本。
更新:
有关如何执行此操作的示例,请查看以下问题:
xaml 中的条件元素取决于绑定内容
An easy reusable solution would be to write a converter which takes your default string as a parameter. This converter can decide to only show the text, when your rate is set.
UPDATE:
For an example how to do this, look at this question:
Conditional element in xaml depending on the binding content