【问题标题】:Triggers in XAMLXAML 中的触发器
【发布时间】:2011-08-22 09:44:42
【问题描述】:

我有一个带有标签的控件.. 和一个布尔依赖属性“IsLink”...所以,如果 IsLink = true,我需要将蓝色前景和光标设置为“手”..

我可以通过绑定来实现,但在这种情况下,我需要编写两个转换器(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>

但它不起作用......有什么想法吗,先生们? :)

【问题讨论】:

    标签: c# wpf xaml binding


    【解决方案1】:

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

    【讨论】:

    • 非常感谢!但是还有一个类似的问题,如果我有两个标签,并且如果另一个标签的内容为空,我想隐藏一个标签,我已经尝试过这样但它没有用
    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-23
      相关资源
      最近更新 更多