【问题标题】:TextBlock foreground binding to IsEnabledTextBlock 前台绑定到 IsEnabled
【发布时间】:2014-11-05 18:42:41
【问题描述】:

我想知道为什么下面的Style 不改变TextBlock Foreground 颜色每当Checkbox IsChecked 状态改变时

<CheckBox Name="checkbox" IsChecked="True"/>

<TextBlock Foreground="LightGray" IsEnabled="{Binding ElementName=checkbox, Path=IsChecked}">
     <TextBlock.Style>
           <Style TargetType="TextBlock">
                 <Style.Triggers>
                       <Trigger Property="TextBlock.IsEnabled" Value="False">
                            <Setter Property="TextBlock.Foreground" Value="Gray" />
                       </Trigger>
                  </Style.Triggers>
            </Style>
      </TextBlock.Style>
 </TextBlock>

【问题讨论】:

    标签: wpf


    【解决方案1】:

    您已设置始终优先于样式触发器的本地值。将属性声明移到样式中,它会起作用,因为样式触发器的优先级高于样式设置器。

    <TextBlock IsEnabled="{Binding ElementName=checkbox, Path=IsChecked}">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Setter Property="Foreground" Value="LightGray"/> <-- HERE
                <Style.Triggers>
                    <Trigger Property="TextBlock.IsEnabled" Value="False">
                        <Setter Property="TextBlock.Foreground" Value="Gray" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    

    继续阅读 - Dependency Property Value Precedence

    【讨论】:

      【解决方案2】:

      更新您现有的样式并在TextBlock的样式上添加默认属性值

      <TextBlock Foreground="LightGray" IsEnabled="{Binding ElementName=checkbox, Path=IsChecked}">
           <TextBlock.Style>
                 <Style TargetType="TextBlock">
                       <Style.Triggers>
                             <Trigger Property="IsEnabled" Value="False">
                                  <Setter Property="Foreground" Value="Gray" />
                             </Trigger>
                        </Style.Triggers>
                  </Style>
                 <Setter Property="Foreground" Value="Black"/>
            </TextBlock.Style>
       </TextBlock>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多