[英]Triggers in XAML
我有一個帶標簽的控件..還有一個布爾依賴屬性“IsLink”...所以,如果IsLink = true,我需要將藍色Foreground和Cursor作為“Hand”。
我可以使用綁定,但在這種情況下我需要編寫兩個轉換器(BoolToCursor和BoolToForeground),但我太懶了:)
所以,我試過這樣的事情:
<Label Name="lblContent" VerticalAlignment="Center" FontSize="14">
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<Trigger SourceName="myControl" Property="IsLink" Value="True">
<!--Set properties here-->
</Trigger>
</Style.Triggers>
</Style>
</Label.Style>
label's text
</Label>
但它不起作用......任何想法,先生們? :)
使用DataTrigger而不是Normal Trigger。檢查下面的代碼
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label Name="lblContent" VerticalAlignment="Center" FontSize="14">
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsLink}"
Value="True">
<Setter Property="Foreground" Value="Blue" />
<Setter Property="Cursor" Value="Hand" />
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
label's text
</Label>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
public Boolean IsLink
{
get { return (Boolean)GetValue(IsLinkProperty); }
set { SetValue(IsLinkProperty, value); }
}
public static readonly DependencyProperty IsLinkProperty =
DependencyProperty.Register("IsLink", typeof(Boolean),
typeof(MainWindow), new UIPropertyMetadata(false));
}
<CheckBox x:Name="IsLink">IsLink</CheckBox>
<Label Name="lblContent"
VerticalAlignment="Center"
FontSize="14">
<Label.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=IsLink, Path=IsChecked}"
Value="true">
<Setter Property="Label.Foreground"
Value="Blue" />
<Setter Property="Label.Cursor"
Value="Hand" />
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
label's text
</Label>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.