Как я могу изменить фоновый цвет определенной кнопки, не создавая новый набор стилей в SharedResources.xaml?
Изображение кнопки
На этом рисунке ниже показаны три кнопки, использующие одинаковые стили из SharedResource.xaml.Моя цель - сделать так, чтобы только вторая кнопка меняла цвет с «WhiteSmoke» на «Purple / Color по моему выбору».
Изображение кнопки
MainWindowViewModel.cs
Эти кнопки имеют стиль в SharedResources.xaml .
protected override void CreateCommands()
{
this.Commands.Add(new CommandViewModel("First Button", new DelegateCommand(p => this.BtnOne())));
this.Commands.Add(new CommandViewModel("Second Button", new DelegateCommand(p => this.BtnTwo())));
this.Commands.Add(new CommandViewModel("Third Button", new DelegateCommand(p => this.BtnThree())));
}
SharedResources.saml
Цвет фона - "WhiteSmoke".Это цвет, который я хочу переопределить только для этой Второй кнопки (или любой конкретной кнопки) без необходимости создания другого набора стилей в SharedResources.xaml.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:System">
<!-- Navigation Style for Buttons -->
<DataTemplate x:Key="CommandsTemplate">
<ItemsControl ItemsSource="{Binding}" HorizontalAlignment="Center">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding Path=Command}" Content="{Binding Path=DisplayName}" Width="180" Height="40" BorderThickness="1" FontSize="20" FontStyle="Oblique" Background="WhiteSmoke" Margin="8,8,0,0">
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
MainWindow.xaml
Кнопки связаны в «Содержании» HeaderedContentControl, а Стили кнопок ограничены в «»ContentTemplate ".
<Window x:Class="System.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:System"
mc:Ignorable="d" Title="{Binding Path=DisplayName}" Height="550" Width="1080">
<!-- Connect this xaml to SharedResources -->
<Window.Resources>
<ResourceDictionary Source="SharedResources.xaml" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="4" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border Height="60">
<HeaderedContentControl Content="{Binding Path=Commands}" ContentTemplate="{StaticResource CommandsTemplate}" />
</Border>
<Border Grid.Row="2">
<HeaderedContentControl Content="{Binding Path=ViewModels}" ContentTemplate="{StaticResource WorkspacesTemplate}" />
</Border>
</Grid>