2010-06-21 109 views
0

是否可以写出这个更简洁/更短的方式?所有它做的是设置3个属性,并采取太多的空间IMO ..WPF XAML样式标记快捷方式?

<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> 

回答

3

排序的,首先你的风格之前定义的转换器:

<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> 
+0

呀,应该工作..但我得到每个属性如下:“WpfGridtest。 ColorConverter'对属性'Background'来说不是一个有效的值......用以前的记法工作得很好......错误消息中的namespace.type是正确的类......它返回SolidColorBrush – 2010-06-21 19:21:42

+0

我将添加一个完整的示例,我2分钟。 – Carlo 2010-06-21 19:36:28

+1

@Sonic Soul:让我知道,如果这可以帮助你更多,完美的工作在我身边。谢谢。 – Carlo 2010-06-21 19:40:03

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在这里进行测试),但尝试一下 - 至少有一点不详细:)。