是否有可能以更光滑/更短的方式写出来?它所做的只是设置3个属性并占用太多的房间IMO ..
<Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource BasicFont}">
<Setter Property="Foreground">
<Setter.Value>
<Binding>
<Binding.Converter>
<local:ForegroundColorConverter />
</Binding.Converter>
</Binding>
</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>
<Binding>
<Binding.Converter>
<local:ColorConverter />
</Binding.Converter>
</Binding>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush">
<Setter.Value>
<Binding>
<Binding.Converter>
<local:ColorConverter />
</Binding.Converter>
</Binding>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:3)
排序,首先你必须在Style之前定义转换器:
<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>
答案 1 :(得分:0)
试试这个:
<Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource BasicFont}">
<Setter Property="Foreground" Value="{Binding Converter={local:ForegroundColorConverter}}"/>
<Setter Property="Background" Value="{Binding Converter={local:ForegroundColorConverter}}"/>
<Setter Property="BorderBrush" Value="{Binding Converter={local:ForegroundColorConverter}}"/>
</Style>
不确定这是否受支持(此处没有VS进行测试),但尝试一下 - 至少要少一点详细:)。