В некотором роде, сначала вы должны определить конвертеры до стиля:
<local:ColorConverter x:Key="colorConverter" />
Тогда вы можете использовать их как StaticResources в своем стиле:
<Setter Property="BorderBrush" Value="{Binding Converter={StaticResource colorConverter}}" />
Полный пример
C #:
using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
// The converter
public class ColorConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Brushes.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="354">
<Window.Resources>
<local:ColorConverter x:Key="colorConverter" />
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{Binding Converter={StaticResource colorConverter}}" />
</Style>
</Window.Resources>
<Grid>
<Button Margin="8" Content="A button" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Window>
Кстати, теперь, когда я об этом думаю, я не уверен, что вы упростили свой код, но вам на самом деле не нужен конвертер для этого. Вы можете установить SolidColorBrush вместо конвертера (если вы не делаете некоторый код в конвертере), что-то вроде этого:
<Window.Resources>
<SolidColorBrush Color="Red" x:Key="redSolidColorBrush" />
<SolidColorBrush Color="White" x:Key="whiteSolidColorBrush" />
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource redSolidColorBrush}" />
<Setter Property="Foreground" Value="{StaticResource whiteSolidColorBrush}" />
</Style>
</Window.Resources>
<Grid>
<Button Margin="8" Content="A button" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>