将TextBlock颜色绑定到Style的Button IsEnabled

时间:2015-05-15 10:27:38

标签: wpf xaml

我正在尝试定义一个Style,可以在Button拥有TextBlock作为其ContentButton拥有IsEnabled时使用TextBlock } = False我想设置Foregroung&#39; s <Button Style="{StaticResource TransparentButtonStyle}" IsEnabled="{Binding IsAllowed}"> <TextBlock Text="Click Me" Style="{StaticResource HyperLinkStyle}"> </TextBlock> </Button> <Style x:Key="TransparentButtonStyle" TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Background="Transparent"> <ContentPresenter/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="HyperLinkStyle" TargetType="TextBlock"> <Setter Property="Foreground" Value="Blue" /> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Cursor" Value="Hand" /> <Setter Property="Foreground" Value="LightBlue" /> <Setter Property="TextDecorations" Value="Underline" /> </Trigger> </Style.Triggers> </Style> 颜色。

TextBlock

我是否可以修改Style&#39; Button以获取父IsEnabled的{​​{1}}值以某种方式设置{ {1}}颜色?

2 个答案:

答案 0 :(得分:2)

如果您想转到Button,可以DataTrigger使用RalativeSource绑定

<Style.Triggers>
   <Trigger Property="IsMouseOver" Value="True">
      <!-- Setters -->
   </Trigger>
   <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsEnabled}" Value="False">
      <!-- Setters -->
   </DataTrigger>
</Style.Triggers>

但是如果你想让你的Style独立于父母的内容,那么Trigger属性上的正常IsEnabled应该可以正常运作

<Style.Triggers>
   <Trigger Property="IsMouseOver" Value="True">
      <!-- Setters -->
   </Trigger>
   <Trigger Property="IsEnabled" Value="False">
      <!-- Setters -->
   </Trigger>
</Style.Triggers>

正常Trigger应该有效,因为IsEnabled值受UIElement.IsEnabledCore影响

  

获取一个值,该值成为派生类中IsEnabled的返回值   ...   此属性的默认实现缓存该值,并计算是否已启用此元素的父元素。 (如果未启用父级,则无法在实际用户界面(UI)中有效启用子元素。)

所以基本上如果父{4}}已被禁用,则儿童Button也会被停用

答案 1 :(得分:2)

您可以将<Trigger Property="IsEnabled" Value="False">添加到HyperLinkStyle,但我建议您改为创建LinkBut​​tonStyle,这样您最终的标记填充就会更加清晰:

  <Button Content="Link text"  Style="{StaticResource LinkButtonStyle}" />

这只会让你的观点变得更清晰......

这是我的LinkBut​​ton模板:

<Style x:Key="LinkButtonStyle" TargetType="Button">
    <Setter Property="Foreground" Value="{StaticResource LinkButtonText}" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <TextBlock x:Name="ContentPresenter" Background="{TemplateBinding Background}"
                           Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}"
                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="ContentPresenter" Property="Foreground" Value="{DynamicResource LinkButtonDisabled}" />
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True" />
                            <Condition Property="IsEnabled" Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter TargetName="ContentPresenter" Property="TextDecorations" Value="Underline"/>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>