WPF XAML样式的标记快捷方式?
有没有可能用更简洁的方式写出来?它所做的只是设置3个属性,占用了太多的空间WPF XAML样式的标记快捷方式?,wpf,xaml,styles,Wpf,Xaml,Styles,有没有可能用更简洁的方式写出来?它所做的只是设置3个属性,占用了太多的空间 <Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource BasicFont}"> <Setter Property="Foreground"> <Setter.Value> <Binding
<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>
试试这个:
<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),但请尝试它-至少不太详细:)。尝试以下方法:
<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),但请尝试一下-这至少有点不太详细:)。某种程度上,首先必须在样式之前定义转换器:
<local:ColorConverter x:Key="colorConverter" />
然后,您可以将它们用作样式中的静态资源:
<Setter Property="BorderBrush" Value="{Binding Converter={StaticResource colorConverter}}" />
完整示例
C#:
使用系统;
使用System.Windows;
使用System.Windows.Data;
使用System.Windows.Media;
命名空间WpfApplication1
{
///
///Window1.xaml的交互逻辑
///
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
}
}
//转换器
公共类ColorConverter:IValueConverter
{
#区域转换器成员
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回刷。红色;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
#端区
}
}
XAML:
顺便说一句,现在我想了想,不确定你是否简化了你的代码,但你实际上并不需要一个转换器。您可以设置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>
某种程度上,首先必须在样式之前定义转换器:
<local:ColorConverter x:Key="colorConverter" />
然后,您可以将它们用作样式中的静态资源:
<Setter Property="BorderBrush" Value="{Binding Converter={StaticResource colorConverter}}" />
完整示例
C#:
使用系统;
使用System.Windows;
使用System.Windows.Data;
使用System.Windows.Media;
命名空间WpfApplication1
{
///
///Window1.xaml的交互逻辑
///
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
}
}
//转换器
公共类ColorConverter:IValueConverter
{
#区域转换器成员
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回刷。红色;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
#端区
}
}
XAML:
顺便说一句,现在我想了想,不确定你是否简化了你的代码,但你实际上并不需要一个转换器。您可以设置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>
yah这应该行。。但是我得到了每个属性的以下信息:“WpfGridtest.ColorConverter”不是属性“Background”的有效值。。。与以前的符号配合得很好。。。错误消息中的namespace.type是正确的类。。。它返回SolidColorBrush,我将添加一个完整的示例,给我2分钟。@Sonic Soul:如果这对您有更多帮助,请告诉我,这对我来说非常有效。谢谢,哇。谢谢差不多就是我所拥有的,但问题显然在我这边,所以我会找到它:)。。谢谢你费了好大劲才做成一个工作的sampleNo,很高兴我能帮上忙。。但是我得到了每个属性的以下信息:“WpfGridtest.ColorConverter”不是属性“Background”的有效值。。。与以前的符号配合得很好。。。错误消息中的namespace.type是正确的类。。。它返回SolidColorBrush,我将添加一个完整的示例,给我2分钟。@Sonic Soul:如果这对您有更多帮助,请告诉我,这对我来说非常有效。谢谢,哇。谢谢差不多就是我所拥有的,但问题显然在我这边,所以我会找到它:)。。感谢您费尽心思制作一个工作的sampleNo,很高兴我能帮忙=)