如何实现事件触发WPF控件作风转变 [英] How to implement event triggered WPF control style change

查看:193
本文介绍了如何实现事件触发WPF控件作风转变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Button控件的样式模板,看起来像:

I have an style template for my Button control that looks like that:

<Style x:Key="myBtnStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="border" Width="100" Height="25"
                        Padding="5,5,5,5" CornerRadius="5,5,5,5"
                        Background="LightGray" BorderBrush="Black" 
                        BorderThickness="1,1,1,1">
                    <ContentPresenter 
                        x:Name="cpButton" 
                        VerticalAlignment="Center"
                        HorizontalAlignment="Center"
                        Width="Auto"  Height="Auto" Margin="-6">
                    </ContentPresenter>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled"  Value="false">
                        <Setter Property="Background" TargetName="border" Value="GhostWhite"></Setter>
                        <Setter Property="BorderBrush" TargetName="border" Value="Gainsboro"></Setter>
                        <Setter Property="Foreground" Value="Gray"></Setter>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter Property="Background" TargetName="border" Value="SkyBlue"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

我的按钮本身的实例:

My button instance itself:

<Button Name="btnResetCount" Content="Reset" Command="{Binding Path=CalcViewModel.ResetCounter}" Style="{StaticResource myBtnStyle}" IsEnabled="{Binding IsButtonCounterEnabled,Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">

有关IsEnabled属性绑定工作正常。所以IsEnabled获取正确的设置从我的ViewModel。

The binding for the IsEnabled Property works fine. So the IsEnabled gets set correct from my ViewModel.

但我的问题是,我的样式IsEnabled &LT; ControlTemplate.Triggers&GT; 没有得到自动在用户界面加载/刷新。我总是要点击任何其他控制第一。一旦我点击另一个控制按钮改变风格也。

But my problem is that my Styles for IsEnabled <ControlTemplate.Triggers> do not get loaded/refreshed automatically in the UI. I always have to click to any other control first. Once i clicked to another control the style of the button changes also.

我能做些什么基础上IsEnabled事件自动更新/刷新按钮的样式/任何控制?

What can i do to automatically update the/refresh the style of the button/any control based on the IsEnabled event?

编辑:

最后我得到它与解决方法的工作。 IM不开心和IM肯定还有更好的方法。但它的作品。

Finally i got it working with an workaround. Im not happy and im sure that there are better ways. But it works.

在我的ViewModel我实现的:

In my ViewModel i implemented:

CommandManager.InvalidateRequerySuggested();

通过调用此方法的控件得到刷新。
如果给出的条件,使按钮,我只是把它在我的一个属性。

By calling this method the controls get refreshed. I just call it in one of my Properties if the condition is given to enable the button..

在此先感谢!

推荐答案

再创建一个触发器,并设置样式IsEnabled =真:

Create one more trigger and set the style for IsEnabled=true :

&LT;触发属性=IsEnabledVALUE =真正的&GT;
                        &LT; setter属性=背景的TargetName =边界值=黑&GT;&LT; /二传手&GT;
                        &LT; setter属性=BorderBrush的TargetName =边界值=黑&GT;&LT; /二传手&GT;
                        &LT; setter属性=前景VALUE =灰色&GT;&LT; /二传手&GT;
                    &LT; /触发&GT;

这篇关于如何实现事件触发WPF控件作风转变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆