C# WPF - 将 UserControl 可见性绑定到属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10607548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF - Bind UserControl visibility to a property
提问by Martin
I have a ListView bound to ObservableCollection. Data are loaded from the internet and then added to collection. The download takes few seconds and I want to indicate user that the data is loading.
我有一个绑定到 ObservableCollection 的 ListView。数据从 Internet 加载,然后添加到集合中。下载需要几秒钟,我想告诉用户数据正在加载。
I created an UserControl that indicates activity. I placed it inside of ControlTemplate.
我创建了一个指示活动的 UserControl。我把它放在 ControlTemplate 里面。
<ControlTemplate x:Key="ListViewControlTemplate1" TargetType="{x:Type ListView}">
<Grid>
<local:ActivityIndicatorControl
HorizontalAlignment="Center"
Height="Auto"
Margin="0"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
I would like to bind Visibility of ActivityIndicatorControlto a property, let's say bool IsLoadingand set it to Visible/Collapsed correspondingly.
我想将 Visibility of 绑定ActivityIndicatorControl到一个属性,假设bool IsLoading并相应地将其设置为 Visible/Collapsed。
Thanks!
谢谢!
采纳答案by davisoa
I would recommend using a IValueConverterto accept your boolean, and return a member of Visibility enumeration.
我建议使用 aIValueConverter来接受您的布尔值,并返回 Visibility 枚举的成员。
Here is a good example of one: http://jeffhandley.com/archive/2008/10/27/binding-converters---visibilityconverter.aspx
这是一个很好的例子:http: //jeffhandley.com/archive/2008/10/27/binding-converters---visibilityconverter.aspx
The XAML would look like this:
XAML 看起来像这样:
First you define a resource for the converter (put this in a resource dictionary):
首先为转换器定义一个资源(将其放入资源字典中):
<local:BooleanToVisibilityConverter x:Key="myBoolToVisibilityConverter" />
And then change your template like this:
然后像这样更改您的模板:
<ControlTemplate x:Key="ListViewControlTemplate1" TargetType="{x:Type ListView}">
<Grid Visibility="{Binding IsLoading, Converter={StaticResource myBoolToVisibilityConverter}}>
<local:ActivityIndicatorControl
HorizontalAlignment="Center"
Height="Auto"
Margin="0"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
回答by Walter Stabosz
Use .NET's built in Converter
使用 .NET 的内置转换器
.NET 3 has a built in BooleanToVisibilityConverter.
.NET 3 有一个内置的BooleanToVisibilityConverter。
(Note: May not be available on all platforms, ex: mobile)
(注意:可能并非在所有平台上都可用,例如:移动)
First add it to your Resources
首先将其添加到您的资源中
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="bool2vis"></BooleanToVisibilityConverter>
</UserControl.Resources>
Then use it on an element
然后在元素上使用它
<Label Visibility="{Binding IsSomeProperty, Converter={StaticResource bool2vis}}" />
Inverting
反转
How do I invert BooleanToVisibilityConverter?
如何反转 BooleanToVisibilityConverter?
If you want to invert the converter (ex: hide the element when your property is true), this answer has a custom implementation of IValueConverterthat supports that via XAML
如果你想反转转换器(例如:当你的属性为真时隐藏元素),这个答案有一个自定义实现IValueConverter,通过 XAML 支持
<Application.Resources>
<app:BooleanToVisibilityConverter
x:Key="BooleanToVisibilityConverter"
True="Collapsed"
False="Visible" />
</Application.Resources>

