将前景色绑定到控制的IsEnabled状态

时间:2012-03-09 17:53:06

标签: c# wpf xaml .net-4.0 styles

我无法将Microsoft.Windows.Controls.Ribbon.RibbonTab对象上的标签颜色绑定到启用状态。

我首先尝试了以下内容:

<Style TargetType="{x:Type r:RibbonTab}">
    <Setter Property="Foreground" Value="White" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsEnabled}" Value="False">
            <Setter Property="Foreground" Value="Gray" />
        </DataTrigger>
    </Style.Triggers>
</Style>

但它对前景没有影响。看起来像ribbonTab库代码中的某些东西以编程方式覆盖前景。

然后我尝试了这个:

<Style x:Key="BaseRibbonTabStyle" TargetType="{x:Type r:RibbonTab}">
    <EventSetter Event="IsEnabledChanged" Handler="RibbonTab_IsEnabledChanged" 
</Style>

private void RibbonTab_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
   if( (bool)e.NewValue == false )
       ((RibbonTab)sender).Foreground = (new System.Windows.Media.BrushConverter()).ConvertFromString("Gray") as System.Windows.Media.Brush;
   else
       ((RibbonTab)sender).Foreground = (new System.Windows.Media.BrushConverter()).ConvertFromString("White") as System.Windows.Media.Brush;
}

但是由于以下编译时错误也失败了:

System.Windows.UIElement.IsEnabledChanged="RibbonTab_IsEnabledChanged_Event" is not valid. 'IsEnabledChanged' must be a RoutedEvent registered with a name that ends with the keyword "Event".

我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分:2)

尝试一个简单的属性触发器:

 <Style TargetType="{x:Type r:RibbonTab}">
     <Style.Triggers>
         <Trigger Property="IsEnabled" Value="False">
             <Setter Property="Foreground" Value="Gray" />
         </Trigger>
     </Style.Triggers>
     <Setter Property="Foreground" Value="White" /> 
 </Style>

答案 1 :(得分:0)

你试过这个:

<Style TargetType="{x:Type r:RibbonTab}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsEnabled}" Value="False">
            <Setter Property="Foreground" Value="Gray" />
        </DataTrigger>
    </Style.Triggers>
    <Setter Property="Foreground" Value="White" />
</Style>