【问题标题】:how to change button background color in c#如何在c#中更改按钮背景颜色
【发布时间】:2018-01-04 02:21:12
【问题描述】:

如何在 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; 但它似乎不起作用。任何帮助将不胜感激。

【问题讨论】:

  • 你想在什么事件上改变它的背景色?
  • 我想知道我是否可以在c#代码中设置按钮的背景颜色而不是在xaml中设置它。我是这个领域的新手,如果我问错了问题,请原谅我。跨度>
  • 一般来说,你想在 xaml 中做东西,而不是在 IMO 后面的 c# 代码中。

标签: c# xaml visual-studio-2015 windows-phone-8.1


【解决方案1】:

你需要修改你的样式如下:

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

这样的事情应该可以工作......

【讨论】:

    【解决方案2】:

    您可以像这样在代码中更改背景颜色:

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

    【讨论】:

    • 它给出错误“当前上下文中不存在名称画笔”
    【解决方案3】:

    您可以使用Data Binding 进行尝试。数据绑定是一个很好的和简单的。一开始你必须读一点,但这是值得的。 特别是对于 MVVM 应用程序。

    【讨论】:

    • 请不要参考一般文档,但要更具体:尝试回答问题。
    猜你喜欢
    • 2012-08-20
    • 2011-06-26
    • 2013-09-15
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    相关资源
    最近更新 更多