在运行时无法更改ControlTemplate的属性

时间:2018-09-29 15:57:47

标签: c# wpf controltemplate

我有一个自定义3状态Button,该状态应在单击时在3个不同状态(颜色)之间循环。但是,我编写的点击处理程序对BorderControlTemplate的背景颜色没有任何影响。

我可能错过了一些非常简单的东西,但是我真的很感激能看到一双新鲜的眼睛。

ControlTemplateButton.Click处理程序如下:

<Style x:Key="FilterButtonStyle"  TargetType="{x:Type local:FilterButton}"
           BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="ButtonBackground" BorderThickness="0" Padding="0"
                    Margin="0" CornerRadius="6"
                    Background="{StaticResource blueShader}" >
                    <ContentPresenter HorizontalAlignment="Center"
                        VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Effect" Value="{StaticResource glowShadow}">
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Foreground" Value="{StaticResource textBrush}"/>
    <Setter Property="Margin" Value="4,4,0,4"></Setter>
    <Setter Property="Padding" Value="4"></Setter>
    <Setter Property="Height" Value="55"></Setter>
    <Setter Property="Width" Value="55"></Setter>
    <Setter Property="FontSize" Value="10"/>
    <Setter Property="FontWeight" Value="Normal"/>
    <Setter Property="TextBlock.TextWrapping" Value="Wrap"></Setter>
    <Setter Property="HasDisabledState" Value="True"></Setter>
    <Setter Property="OnColour" Value="{StaticResource greenShader}"></Setter>
    <Setter Property="OffColour" Value="{StaticResource blueShader}"></Setter>
    <Setter Property="DisabledColour" Value="{StaticResource redShader}"></Setter>
</Style>
private void FilterButton_Click(object sender, RoutedEventArgs e)
{
    if (!this.CanChangeState) { return; }

    // Get child border element.
    Border border = this.Template.LoadContent() as Border;

    // Cycle through button states.
    switch (this.ButtonState)
    {
        case ButtonStates.Off:
            this.ButtonState = ButtonStates.On;
            border.Background = this.OnColour;
            break;

        case ButtonStates.On:
            if (this.HasDisabledState)
            {
                this.ButtonState = ButtonStates.Disabled;
                border.Background = this.DisabledColour;
            }
            else
            {
                this.ButtonState = ButtonStates.Off;
                border.Background = this.OffColour;
            }
            break;

        case ButtonStates.Disabled:
            this.ButtonState = ButtonStates.Off;
            border.Background = this.OffColour;
            break;
    }
}

1 个答案:

答案 0 :(得分:0)

没关系,我发现一种更好的处理方法是使用ControlTemplate触发器基于ButtonState属性更改Button的颜色。

    <Style x:Key="FilterButtonStyle"  TargetType="{x:Type local:FilterButton}"
       BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="ButtonBackground" BorderThickness="0" Padding="0" Margin="0" CornerRadius="6"
                    Background="{StaticResource blueShader}" >
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Effect" Value="{StaticResource glowShadow}">
                        </Setter>
                    </Trigger>
                    <Trigger Property="local:FilterButton.ButtonState" Value="On">
                        <Setter TargetName="ButtonBackground" Property="Background" Value="{StaticResource greenShader}"></Setter>
                    </Trigger>
                    <Trigger Property="local:FilterButton.ButtonState" Value="Off">
                        <Setter TargetName="ButtonBackground" Property="Background" Value="{StaticResource blueShader}"></Setter>
                    </Trigger>
                    <Trigger Property="local:FilterButton.ButtonState" Value="Disabled">
                        <Setter TargetName="ButtonBackground" Property="Background" Value="{StaticResource redShader}"></Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Foreground" Value="{StaticResource textBrush}"/>
    <Setter Property="Margin" Value="4,4,0,4"></Setter>
    <Setter Property="Padding" Value="4"></Setter>
    <Setter Property="Height" Value="55"></Setter>
    <Setter Property="Width" Value="55"></Setter>
    <Setter Property="FontSize" Value="10"/>
    <Setter Property="FontWeight" Value="Normal"/>
    <Setter Property="TextBlock.TextWrapping" Value="Wrap"></Setter>
    <Setter Property="HasDisabledState" Value="True"></Setter>
</Style>