用角圆改变颜色按钮

时间:2016-06-30 20:27:28

标签: c# wpf xaml

我找到了这段代码: XAML:

<Button Content="Click Me!" Margin="209,135,263.4,140.8" Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click_1">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="bdr_main" CornerRadius="3" Margin="0" BorderThickness="1" BorderBrush="Black" Background="LightGray">
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="bdr_main" Property="Background" Value="LightGreen"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="bdr_main" Property="Background" Value="Red"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Button.Template>
    </Button>

可以用WTF代码改变背景颜色吗? 我点击了按钮,背景颜色发生变化。

使用普通按钮 我可以使用这段代码:             bdr_main.Background = new SolidColorBrush(Colors.Red); 要么             bdr_main.Background = Brushes.Red; 但他没有工作 你能救我吗?

2 个答案:

答案 0 :(得分:0)

编辑已更改为每个OP跟进的ToggleButton

只需在触发器中添加一个setter。

        <ToggleButton Content="Click Me!" Margin="209,135,263.4,140.8" Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click_1">
            <ToggleButton.Template>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border x:Name="bdr_main" CornerRadius="3" Margin="0" BorderThickness="1" BorderBrush="Black" Background="LightGray">
                        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="bdr_main" Property="Background" Value="LightGreen"/>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter TargetName="bdr_main" Property="Background" Value="Red"/>

                            //Add line below
                            <Setter Property="Background" Value="Red" />

                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </ToggleButton.Template>
        </ToggleButton>

编辑 你不再需要这样做

如果你想在click事件处理程序的代码后面执行它;请注意,此行为与上面的触发器不同,背景将保持红色。

Button_Click_1(sender, e)
{
    ((Button)sender).Background = Colors.Red;

答案 1 :(得分:0)

您可以使用ToggleButton及其IsChecked属性

<ToggleButton Content="Click Me!" Margin="209,135,263.4,140.8">
    <ToggleButton.Template>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
            <Border x:Name="bdr_main" CornerRadius="3" Margin="0" 
                    Background="LightGray" BorderThickness="1" BorderBrush="Black">
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="bdr_main" Property="Background" Value="LightGreen"/>
                </Trigger>
                <Trigger Property="IsChecked" Value="True">
                    <Setter TargetName="bdr_main" Property="Background" Value="Red"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ToggleButton.Template>
</ToggleButton>

修改

如果您想从后面的代码更改背景颜色,您需要实现它的模板绑定

prop="{TemplateBinding Background}

将XAML剥离到骨骼

<ToggleButton Click="Button_Click" Content="Click Me!"
              Background="LightGray" Margin="209,135,263.4,140.8">
    <ToggleButton.Template>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
            <Border x:Name="bdr_main" CornerRadius="3" Margin="0" BorderThickness="1" BorderBrush="Black" 
                    Background="{TemplateBinding Background}">
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" />
            </Border>
        </ControlTemplate>
    </ToggleButton.Template>
</ToggleButton>

现在你可以

using System.Windows.Controls.Primitives;

...

public void Button_Click(object sender, EventArgs e)
{
    var butt = sender as ToggleButton;
    if ((bool)butt.IsChecked)
        butt.Background = Brushes.Red;
    else
        butt.Background = Brushes.LightGray;
}

最好在ControlTemplate中处理MouseOver事件,以获得与我在第一篇文章中相同的行为,您可以将其更改为

<ControlTemplate TargetType="{x:Type ToggleButton}">
    <Border x:Name="bdr_main" CornerRadius="3" Margin="0" BorderThickness="1" BorderBrush="Black" 
            Background="{TemplateBinding Background}">
        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" />
    </Border>
    <ControlTemplate.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsMouseOver" Value="True" />
                <Condition Property="IsChecked" Value="False" />
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter TargetName="bdr_main" Property="Background" Value="LightGreen" />
            </MultiTrigger.Setters>
        </MultiTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>