Вам нужно изменить свой стиль следующим образом:
<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="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="https://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));
}
}
}
Что-то вроде этого должно работать...
28.07.2017