简体   繁体   English

WPF,Telerik:禁用时更改控件样式

[英]WPF, Telerik: Change control style when disabled

I have a trouble with Telerik control. 我在Telerik控制上遇到了麻烦。

<Style x:Key="RadDropDownButtonStyle" TargetType="telerik:RadDropDownButton">
<Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="telerik:RadDropDownButton">
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Foreground"  Value="Red" />...

So, this handles Disabled property. 因此,这将处理Disabled属性。 Text changes its color, but it is not contrast, something like watermark. 文本会更改其颜色,但不会改变对比度,例如水印。

How can I disable this? 如何禁用此功能? And make disabled control more contrast? 并让禁用控件更具对比度?

Text changes its color, but it is not contrast, something like watermark. 文本会更改其颜色,但不会改变对比度,例如水印。

For future references, this is caused by the Opacity property. 为了将来参考,这是由Opacity属性引起的。 When the opacity is lower than 1 it becomes lighter. 当不透明度小于1时,它会变浅。

The problem you are describing is something very familiar. 您正在描述的问题非常熟悉。 And I'm afraid there isn't any great way to get rid of this, so much so that the best advise that I can give you is to not even bother. 而且,恐怕没有什么好方法可以消除这个问题,以至于我最好能给您的建议就是不要再打扰。 If anything, it will just make using the themes and styles a real pain. 如果有的话,这只会使主题和样式的使用成为现实。

However there is an easy way to get your desired behaviour by replacing the IsEnabled="False" property with IsHitTestVisible="False" and Focusable="False" 但是,有一种简单的方法可以通过将IsEnabled="False"属性替换为IsHitTestVisible="False"Focusable="False"来获得所需的行为。
These will make it impossible to click or focus the control by keyboard navigation, basically making it disabled. 这些将使您无法通过键盘导航来单击或集中控制该控件,从而使其基本处于禁用状态。 Now you can add some more style properties to make it look the way you believe a disabled control should look like. 现在,您可以添加更多样式属性,以使其看起来像您认为已禁用控件的外观。 By for example setting the wanted Foreground and Background or you could even use an Opacity closer to 1 (ex: 0.9) which will make it less dark than the original but still dark enough to read properly. 例如,通过设置所需的“ 前景”和“ 背景”,或者甚至可以使用不透明度接近1(例如0.9)的方法,这将使其不比原始图像更暗,但仍然足够黑暗以至于无法正确读取。

I hope this helps you with your current problem, just leave a comment if you want me to clarify further. 希望这对您当前的问题有所帮助,如果您想让我进一步澄清,请发表评论。


EDIT 1: Apperently can overwrite the opacity change by using your own DataTemplate for the control. 编辑1:显然可以通过使用控件自己的DataTemplate覆盖不透明度更改。 How to have 100% opacity even when control is disabled in wpf C# 即使在WPF C#中禁用控件,如何也具有100%的不透明度


EDIT 2: I'll give you an example of how to use the other properties correctly. 编辑2:我将为您提供一个有关如何正确使用其他属性的示例。

This is how you would define a disabled button normally, and doing so will make the text lighter and less readable. 通常,这就是定义禁用按钮的方式,这样做会使文本更浅且可读性更差。

<!-- Simple disabled button -->
<telerik:RadButton Content="Test Button 1" IsEnabled="False" />

<!-- Button with binding on IsEnabled -->
<telerik:RadButton Content="Test Button 2" IsEnabled="{Binding MyBinding}" />

Now I'll show you how you can mimic these results by using the properties IsHitTestVisible and Focusable . 现在,我将向您展示如何使用属性IsHitTestVisibleFocusable模仿这些结果。

<!-- Simple disabled button -->
<telerik:RadButton Content="Test Button 1" IsHitTestVisible="False" Focusable="False" />

<!-- Button with binding on IsEnabled -->
<telerik:RadButton Content="Test Button 2" IsHitTestVisible="{Binding MyBinding}" Focusable="{Binding MyBinding}" />

In the above examples, the buttons will look as if they are still enabled. 在以上示例中,按钮看起来好像仍处于启用状态。 However you won't be able to focus or click them. 但是,您将无法聚焦或单击它们。 Of course we do want to see some difference to be able to tell that they can't be used. 当然,我们确实希望看到一些区别,以便能够断言它们无法使用。

<!-- Styled disabled button -->
<telerik:RadButton Content="Test Button 1" IsHitTestVisible="False" Focusable="False" >
    <telerik:RadButton.Style>
        <Style TargetType="telerik:RadButton">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsHitTestVisible}" Value="False">
                    <Setter Property="Opacity" Value="0.8"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </telerik:RadButton.Style>
</telerik:RadButton>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM