根据ItemsSource中的数据类型,wpf datagrid中的不同的单元格样式 [英] Different cell styling in a wpf datagrid depending on datatype in the ItemsSource
问题描述
我想知道是否可以根据ItemsSource集合中项目的类型更改wpf datagrid中的列的样式。我有一个从wpf工具包的wpf datagrid
根据ItemsSource集合中的项目类型,网格中的单个行应该进行样式化。所以所有项目都是相同的基类类型,但是一些派生类型的列应该得到不同的样式化。
这是可能吗?
谢谢: - )
是的,可以通过多种方式进行。我要去的是编写自己的定制类型开关转换器,根据输入类型选择一个值。像这样:
public class TypeSwitchConverter:Dictionary< Type,object>,IValueConverter
{
public object转换(对象值,类型targetType,对象参数,.CultureInfo文化)
{
foreach(var mapping in this)
{
if(mapping.Key.IsAssignableFrom GetType()))
{
return mapping.Value;
}
}
返回null;
}
public object ConvertBack(object value,Type targetType,object parameter,CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后为样式
,您的单元格模板中的顶级元素,并根据需要使用上述转换器进行绑定。这是一个简化的示例,它使用 ListBox
中的样式项目:
< ListBox ItemsSource ={Binding}>
< ListBox.ItemTemplate>
< DataTemplate>
< TextBlock Text ={Binding}>
< TextBlock.Style>
< Binding>
< Binding.Converter>
< my:TypeSwitchConverter>
< Style x:Key ={x:Type cor:Int32}TargetType ={x:Type TextBlock}>
< Setter Property =BackgroundValue =Red/>
< / Style>
< Style x:Key ={x:Type cor:String}TargetType ={x:Type TextBlock}>
< Setter Property =BackgroundValue =Green/>
< / Style>
< Style x:Key ={x:Type sys:Uri}TargetType ={x:Type TextBlock}>
< Setter Property =BackgroundValue =Blue/>
< / Style>
< / my:TypeSwitchConverter>
< /Binding.Converter>
< / Binding>
< /TextBlock.Style>
< / TextBlock>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>
I am wondering if it was possible to change the styling of a column in a wpf datagrid depending on the type of item in the ItemsSource collection.
I have a wpf datagrid from the wpf toolkit. The single rows in the grid should be styled depending of the type of item from the ItemsSource collection. So all items are of the same base class type but the columns of some derived types should get a different stylization.
Is this possible?
Thank you :-)
Yes, it is possible to do it in several ways. The one I would go for is writing your own custom "typeswitch" converter that selects a value depending on type of input. Like this:
public class TypeSwitchConverter : Dictionary<Type, object>, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, .CultureInfo culture)
{
foreach (var mapping in this)
{
if (mapping.Key.IsAssignableFrom(value.GetType()))
{
return mapping.Value;
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
And then use a binding for the Style
of the top-level element in template for your cell, and use the above converter for that binding as needed. Here's a simplified example that styles items in a ListBox
using it:
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}">
<TextBlock.Style>
<Binding>
<Binding.Converter>
<my:TypeSwitchConverter>
<Style x:Key="{x:Type cor:Int32}" TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="Red" />
</Style>
<Style x:Key="{x:Type cor:String}" TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="Green" />
</Style>
<Style x:Key="{x:Type sys:Uri}" TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="Blue" />
</Style>
</my:TypeSwitchConverter>
</Binding.Converter>
</Binding>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这篇关于根据ItemsSource中的数据类型,wpf datagrid中的不同的单元格样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!