2017-07-28 136 views
0

如何在Windows Phone應用程序中以編程方式更改按鈕背景顏色。 這是我的xaml代碼。如何更改c中的按鈕背景顏色#

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

我試過使用「using System.Drawing」yourButtonName.BackColor = Color.Red; 但它似乎沒有工作。任何幫助將不勝感激。

+0

您想在哪種情況下進行背景顏色更改? –

+0

我想知道如果我可以在c#代碼中設置按鈕的背景顏色,而不是在xaml中設置它。我是這個領域的新手,如果我問的是錯誤的問題,請原諒我。 – Rachel

+0

一般來說,你想在xaml而不是後面的c#代碼中做東西,IMO。 – Stuart

回答

1

你可以改變背景顏色這樣的代碼:

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

它提供了一個錯誤「名稱筆刷在當前上下文中不存在」 – Rachel

+0

對不起,我編輯了答案。 – user3390116

2

您需要修改自己的風格如下:

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

水木清華這樣應該工作...

+0

我試過了你的方法,但是沒有工作.. – Rachel

+0

對不起!我編輯了答案。 –

0

您可以Data Binding嘗試。數據綁定是一個不錯的和明智的。一開始你必須讀一點,但它是值得的。 尤其適用於MVVM應用程序。

+1

請不要參考一般文件,但要更具體:嘗試回答這個問題。 – kloarubeek

相關問題