排序的,首先你的风格之前定义的转换器:
<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>
呀,应该工作..但我得到每个属性如下:“WpfGridtest。 ColorConverter'对属性'Background'来说不是一个有效的值......用以前的记法工作得很好......错误消息中的namespace.type是正确的类......它返回SolidColorBrush – 2010-06-21 19:21:42
我将添加一个完整的示例,我2分钟。 – Carlo 2010-06-21 19:36:28
@Sonic Soul:让我知道,如果这可以帮助你更多,完美的工作在我身边。谢谢。 – Carlo 2010-06-21 19:40:03