繁体   English   中英

如何在C#中更改按钮背景颜色

[英]how to change button background color in c#

如何在Windows Phone应用程序中以编程方式更改按钮背景色。 这是我的xaml代码。

<Style TargetType="Button" x:Key="TabButtonLast">
        <Setter Property="Foreground" Value="Navy"/>
        <Setter Property="Background" Value="Green" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="15,15,15,15" Background="Green" >
                        <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="Center"  VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
<Button Name="btnNext" Style="{StaticResource TabButtonLast}" Content="Next" Height="23" HorizontalAlignment="Left" Margin="131,311,0,0" VerticalAlignment="Top" Width="75" Click="btnNext_click" />

我尝试使用“使用System.Drawing” yourButtonName.BackColor = Color.Red; 但这似乎无效。任何帮助将不胜感激。

您需要按如下方式修改样式:

<Style TargetType="Button" x:Key="TabButtonLast">
        <Setter Property="Foreground" Value="Navy"/>
        <Setter Property="Background"
    Value="{Binding Background, RelativeSource={RelativeSource Self}}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="15,15,15,15" Background="{TemplateBinding Background}">
                        <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="Center"  VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

1)如果您想要静态背景:

<Button Background="Red" Name="btnNext" Style="{StaticResource TabButtonLast}" Content="Next" Height="23" HorizontalAlignment="Left" Margin="131,311,0,0" VerticalAlignment="Top" Width="75" Click="btnNext_click" />

2)要从代码更改背景色:

private void ChangeButtonColor()
{
btnNext.Background = "Red";
}

3)使用MVVM方法的例子:

“前端”:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication3"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

        <Style TargetType="Button" x:Key="TabButtonLast">
            <Setter Property="Foreground" Value="Navy"/>
            <Setter Property="Background"
        Value="{Binding Background, RelativeSource={RelativeSource Self}}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border CornerRadius="15,15,15,15" Background="{TemplateBinding Background}">
                            <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="Center"  VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <Button Style="{StaticResource TabButtonLast}" Content="CHANGE COLOR" Background="{Binding BtnBackColor}" 
                  Margin="50" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="Button_Click" />
    </Grid>

</Window>

“后端”:

  using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media;

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public Brush BtnBackColor { get; set; } = new SolidColorBrush(Colors.Red);
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Random r = new Random();

            //Without Binding variant
            //btnNext.Background = new SolidColorBrush(Color.FromRgb((byte)r.Next(1, 255),
            //  (byte)r.Next(1, 255), (byte)r.Next(1, 233)));

            //MVVM approach variant
            BtnBackColor = new SolidColorBrush(Color.FromRgb((byte)r.Next(1, 255),
                (byte)r.Next(1, 255), (byte)r.Next(1, 233)));
            OnPropertyChanged("BtnBackColor");
        }
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

这样的东西应该可以工作...

您可以通过以下代码更改背景色:

btnNext.Background = new SolidColorBrush(Windows.UI.Colors.Red);

您可以尝试使用数据绑定 数据绑定非常简单。 开始时,您必须阅读一些内容,但这是值得的。 特别适用于MVVM应用程序。

暂无
暂无

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

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