【问题标题】:change color button with corner rounder使用圆角器更改颜色按钮
【发布时间】:2016-06-30 20:27:28
【问题描述】:

我找到了这段代码: 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 = 画笔.Red; 但他不工作 你能帮帮我吗?

【问题讨论】:

  • 更改边框以使用 TemplateBinding 而不是 Background="LightGray"
  • 对不起,您的代码运行良好,谢谢您有与 WPF 代码相同的解决方案吗?
  • 你的 Xaml 看起来像 WPF ???没有。
  • 是的,但是像一些类似的例子:bdr_main.Background = new SolidColorBrush(Colors.Red);我会有更好的掌握

标签: c# wpf xaml


【解决方案1】:

您可以使用 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>

编辑

如果你想从后面的代码中改变背景颜色,你需要实现它的 TemplateBinding

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;
}

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

<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>

【讨论】:

  • 对不起,您的代码运行良好,谢谢您有与 WPF 代码相同的解决方案吗?
  • 你好,正是我想要的谢谢你的帮助。
【解决方案2】:

根据 OP 跟进,Edit 更改为 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;

【讨论】:

  • 感谢您的帮助,但它是一样的,我用 wpf 代码搜索了一个解决方案,例如:带有 .Background = new SolidColorBrush(Colors.Blue); 的按钮名称但 WPF 无法识别名称 bdr_main
  • 你能描述一下它是如何不工作的吗?当你按下按钮时它不会变红?松开鼠标后,颜色变回浅灰色。您想在松开按钮后保持红色吗?
  • 它是一样的,因为当我点击按钮时,红色出现,但在浅灰色重新返回后对于你的代码我可能希望当我点击按钮时红色保持不变在同一按钮或其他按钮上搜索更改颜色的可能代码。使用 WPF 代码可能更容易吗?
  • 您的代码:Button_Click_1(sender, e) { ((Button)sender).Background = Colors.Red;使用普通按钮,但没有使用按钮代码,请问您有其他解决方案吗?
  • 我恢复了这种情况您好,您使用 xamp 的代码有效。你的 WPF 代码不起作用也许我不明白你能解释一下你的 wpf 代码吗?
猜你喜欢
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多