【发布时间】:2013-05-22 01:41:39
【问题描述】:
我想根据绑定设置 TextBox 元素的可见性。
例子:
具有两种产品价格值的文本框
<TextBox Grid.Column="1" Grid.Row="3" Text="{Binding PriceProduct0, Mode=TwoWay}" />
<TextBox Grid.Column="2" Grid.Row="3" Text="{Binding PriceProduct1, Mode=TwoWay}" />
在我的模型中,我从 WCF 服务中查询必要的值并调用 NotifyPropertyChanged 来更新我的视图。
问题:在某些情况下,我的属性没有值。在这种情况下,我想隐藏 UI 元素。有没有简单的方法,比如引发事件?
大多数属性都是双精度或布尔值,所以我不能将它们设置为 NULL。
解决方案:
感谢 Ahmed 和 DHN,我找到了解决方案!
这里所有步骤:
- 将所有不可为空的属性更改为可空值,例如
public double? PriceProduct0 - 如果找不到命名空间,请添加对以下 .dll 的引用
Microsoft.TeamFoundation.Controls.WPF.Converters:<VSHome>\Common7\IDE\PrivateAssemblies\Microsoft.TeamFoundation.Controls.dll - 将
xmlns:myConverters="clr-namespace:Microsoft.TeamFoundation.Controls.WPF.Converters;assembly=Microsoft.TeamFoundation.Controls"添加到<Page>属性 - 添加
<Page.Resources>
<myConverters:NullToVisibleConverter x:Key="NullToVisibleConverter" />
</Page.Resources> - 将以下可见性参数添加到
<TextBox>属性:Visibility="{Binding PriceProduct0, Converter={StaticResource NullToVisibleConverter}, ConverterParameter='invert' }"
【问题讨论】:
标签: c# wpf binding visibility