WPF - 绑定用户控件可见性属性 [英] WPF - Bind UserControl visibility to a property
问题描述
我绑定到的ObservableCollection一个ListView。数据从网络加载,然后加入到集合中。下载需要几秒钟,我想表明数据加载用户。
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.
我创建了一个用户控件,表示活动。我把它的ControlTemplate内。
I created an UserControl that indicates activity. I placed it inside of ControlTemplate.
<ControlTemplate x:Key="ListViewControlTemplate1" TargetType="{x:Type ListView}">
<Grid>
<local:ActivityIndicatorControl
HorizontalAlignment="Center"
Height="Auto"
Margin="0"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
我要绑定 ActivityIndicatorControl
的可见性属性,比方说布尔IsLoading
并将其设置为可见/折叠水涨船高。
I would like to bind Visibility of ActivityIndicatorControl
to a property, let's say bool IsLoading
and set it to Visible/Collapsed correspondingly.
谢谢!
推荐答案
我会推荐使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx\"><$c$c>IValueConverter$c$c>接受你的布尔值,并返回能见度枚举的成员。
I would recommend using a IValueConverter
to accept your boolean, and return a member of Visibility enumeration.
下面是一个很好的例子:<一href=\"http://jeffhandley.com/archive/2008/10/27/binding-converters---visibilityconverter.aspx\">http://jeffhandley.com/archive/2008/10/27/binding-converters---visibilityconverter.aspx
Here is a good example of one: http://jeffhandley.com/archive/2008/10/27/binding-converters---visibilityconverter.aspx
该XAML是这样的:
首先,定义资源的转换器(把这个资源字典):
First you define a resource for the converter (put this in a resource dictionary):
<local:BooleanToVisibiltyConverter 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>
这篇关于WPF - 绑定用户控件可见性属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!