Triggers in XAML
14,552
Solution 1
Use a DataTrigger instead of Normal Trigger.Check the code below
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));
}
Solution 2
<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>
Author by
Artem Makarov
Updated on August 08, 2022Comments
-
Artem Makarov over 1 year
I have a control with label.. And a boolean dependency property "IsLink"... So, if IsLink = true, I need to make blue Foreground and Cursor as "Hand" as well..
I can make it with bindings, but in this case I need to write two Converters (BoolToCursor and BoolToForeground), but I'm too lazy for that :)
So, I've tryed smth like that:
<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>
But it doesn't work... Any ideas, gentlemens? :)
-
Artem Makarov almost 13 yearsThanks a lot! But one more similar question, if I have two labels and I'd like to hide one label if another's Content is null, I've tried like this but it didn't work