【问题标题】:Change WPF control's ControlTemplate to not be disabled将 WPF 控件的 ControlTemplate 更改为不禁用
【发布时间】:2013-01-19 09:27:54
【问题描述】:

我有一个 Devexpress DateEdit 并添加了一个触发器,用于何时 IsEnabled=FalseControlTemplate 更改为 Label。这一切都很好,但我的问题是,LabelText 仍然是Grayed out(已禁用)。

我的风格:

<Style x:Key="DateTimeDropDownStyle" TargetType="{x:Type dxe:DateEdit}">
        <Setter Property="Mask" Value="dd MMM yyyy"/>
        <Setter Property="MaskUseAsDisplayFormat" Value="True"/>
        <Style.Triggers>
            <Trigger Property="dxe:DateEdit.IsEnabled" Value="False">
                <Setter Property="dxe:DateEdit.Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Label Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, StringFormat={}{0:dd MMM yyyyy}}"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

所以,我的问题是,如何更改 Style 以使 Label 不被禁用?

【问题讨论】:

  • 想到2个想法,第一个在标签级别将Label本身设置为IsEnabled = true,第二个使用IsReadOnly属性而不是IsEnabled。
  • @eranotzap 我确实尝试设置IsEnabled=True,但没有奏效。在这里设置IsReadOnly 属性不是一个选项。我仅限于IsEnabled 属性。

标签: wpf xaml styles label controltemplate


【解决方案1】:

尝试在模板中的Label 上设置Foreground

如果没有帮助,您必须编辑标签的控制模板。一个基本的是:

<ControlTemplate TargetType="{x:Type Label}">
    <Border Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}">
        <ContentPresenter Margin="{TemplateBinding Padding}"/>    
    </Border>
    <ControlTemplate.Triggers>
        <!--This is the trigger to remove-->
        <Tirgger Property="IsEnabled"
                 Value="False">
            <Setter Property="Foreground"
                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

【讨论】:

  • 设置Foreground 没有帮助。文本仍为“灰色”(标签已禁用)。再次设置ControlTemplate 就可以了。谢谢=)
猜你喜欢
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多