【问题标题】:UserControl property trigger for child control子控件的 UserControl 属性触发器
【发布时间】:2011-06-06 05:18:57
【问题描述】:

根据我的 UserControl 的 IsEnabled 属性(真/假),我希望其中的控件具有不同的颜色。我想用 XAML 的“魔力”来做到这一点。

<UserControl.Resources>
    <Style x:Key="EnableDependent" TargetType="{x:Type Shape}">
        <Style.Triggers>
            <Trigger Property="{Binding Path=IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Value="True">
                <Setter Property="Stroke" Value="White" />
            </Trigger>
            <Trigger Property="{Binding Path=IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Value="False">
                <Setter Property="Stroke" Value="Black" />
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

样式应用于绘制路径的 ViewBox:

    <Viewbox  Grid.Column="3" Width="18" Margin="5,5,2,5" MouseEnter="Dispatch_MouseEnter" DockPanel.Dock="Right" Stretch="Uniform">
        <Path Data="M0,1 L4,1 M2,0 L4,1 L2,2" Stretch="Fill" StrokeThickness="3" Width="12" Height="12" Style="{StaticResource EnableDependent}" />
    </Viewbox>

我得到一个运行时异常,无法在触发器的“属性”属性中设置绑定。

那么有什么办法做到这一点呢?

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    使用DataTrigger 而不是用于内部属性更改的普通Trigger,它有一个Binding-属性,您可以在其中执行此操作。

    <Style x:Key="EnableDependent" TargetType="{x:Type Shape}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Value="True">
                <Setter Property="Stroke" Value="White" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Value="False">
                <Setter Property="Stroke" Value="Black" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-23
      • 2011-08-04
      • 2013-01-14
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      • 2014-04-23
      • 2015-04-01
      相关资源
      最近更新 更多