Silverlight之DataTrigger用法
一、在Project添加两个dll引用:
1.System.Windows.Interactivity
2.Microsoft.Expression.Interactions
二、在前台xaml里面添加:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
三、在目标控件用法如下:
<ListBox x:Name="lbxUsers" Grid.Row="1" ItemsSource="{Binding Path=UserModels}" SelectedItem="{Binding Path=SelectedUserModel,Mode=TwoWay}" Margin="0,5,0,0">
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding Path=IsLoaded}" Value="True">
<ei:ChangePropertyAction TargetObject="{Binding ElementName=lbxUsers}" TargetName="Visibility" PropertyName="Visibility" Value="Visible"></ei:ChangePropertyAction>
<ei:ChangePropertyAction TargetObject="{Binding ElementName=tblHeader}" TargetName="Foreground" PropertyName="Foreground" Value="Red"></ei:ChangePropertyAction>
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding Path=IsLoaded}" Value="False">
<ei:ChangePropertyAction TargetObject="{Binding ElementName=lbxUsers}" TargetName="Visibility" PropertyName="Visibility" Value="Collapsed">
</ei:ChangePropertyAction>
</ei:DataTrigger>
</i:Interaction.Triggers>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=UserName}"/>
<TextBlock Text="{Binding Path=Password}" Margin="10,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
解析: IsLoaded是绑定的obj的一个property,类型为Boolean。上述例子展示了IsLoaded属性改变Visibility和Foreground两个属性。
以上内容纯个人理解,如有错误欢迎指出。
浙公网安备 33010602011771号