XAML中的触发器

Art*_*rov 7 c# wpf xaml binding

我有一个带标签的控件..还有一个布尔依赖属性"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>
Run Code Online (Sandbox Code Playgroud)

但它不起作用......任何想法,先生们?:)

bij*_*iju 9

使用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));


    }
Run Code Online (Sandbox Code Playgroud)