我有一个绑定到VM集合的网格。在DataTemplateSelector
使用DataGridTemplateColumn
我将整个VM作为数据项时,如何将其缩小到特定属性值(否则我必须创建' DataTemplateSelector& #39;对于每个VM或使用接口,两者都太麻烦了??
看见Bind a property to DataTemplateSelector,但它看起来像是一个讨厌的解决方法。
答案 0 :(得分:2)
您可以在DataTemplateSelector的派生类中使用Expressions Trees,我称之为PropertyTemplateSelector
。这是它的代码:
public abstract class PropertyTemplateSelector : DataTemplateSelector
{
private Delegate getPropertyValue;
private string propertyName;
private Type itemType;
public string PropertyName
{
get
{
return propertyName;
}
set
{
propertyName = value;
}
}
public Type ItemType
{
get
{
return itemType;
}
set
{
itemType = value;
}
}
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (ItemType.IsInstanceOfType(item))
{
if (getPropertyValue == null)
{
System.Linq.Expressions.ParameterExpression instanceParameter =
System.Linq.Expressions.Expression.Parameter(item.GetType(), "p");
System.Linq.Expressions.MemberExpression currentExpression =
System.Linq.Expressions.Expression.PropertyOrField(instanceParameter, PropertyName);
System.Linq.Expressions.LambdaExpression lambdaExpression =
System.Linq.Expressions.Expression.Lambda(currentExpression, instanceParameter);
getPropertyValue = lambdaExpression.Compile();
}
return SelectTemplateImpl(getPropertyValue.DynamicInvoke(item), container);
}
return base.SelectTemplate(item, container);
}
protected abstract DataTemplate SelectTemplateImpl(object propertyValue, DependencyObject container);
}
您可以使用自己的逻辑扩展此类,只需实现SelectTemplateImpl
方法即可。如您所见,PropertyTemplateSelector
将项目对象缩小为特定属性值(传递给SelectTemplateImpl
方法)。例如,我以这种方式创建了NameTemplateSelector
:
public class NameTemplateSelector : PropertyTemplateSelector
{
protected override DataTemplate SelectTemplateImpl(object propertyValue, DependencyObject container)
{
string name = (string)propertyValue;
if (name != null && name.StartsWith("A", StringComparison.OrdinalIgnoreCase))
{
return (DataTemplate)App.Current.MainWindow.FindResource("VipName");
}
return (DataTemplate)App.Current.MainWindow.FindResource("NormalName");
}
}
您可以在XAML中轻松使用这些模板选择器
<Window.Resources>
<DataTemplate x:Key="NormalName">
<TextBlock Text="{Binding Mode=OneWay, Path=Name}" Margin="3" />
</DataTemplate>
<DataTemplate x:Key="VipName">
<TextBlock Text="{Binding Mode=OneWay, Path=Name}" Margin="3" Foreground="Red" FontWeight="Bold" />
</DataTemplate>
<DataTemplate x:Key="NormalSurname">
<TextBlock Text="{Binding Mode=OneWay, Path=Surname}" Margin="3" />
</DataTemplate>
<DataTemplate x:Key="VipSurname">
<TextBlock Text="{Binding Mode=OneWay, Path=Surname}" Margin="3" Foreground="Green" FontStyle="Italic" />
</DataTemplate>
<local:NameTemplateSelector x:Key="NameTemplateSelector" PropertyName="Name" ItemType="{x:Type local:Person}" />
<local:SurnameTemplateSelector x:Key="SurnameTemplateSelector" PropertyName="Surname" ItemType="{x:Type local:Person}" />
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Path=People, Mode=OneWay}" AutoGenerateColumns="False"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name"
CellTemplateSelector="{StaticResource NameTemplateSelector}" />
<DataGridTemplateColumn Header="Surname"
CellTemplateSelector="{StaticResource SurnameTemplateSelector}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
我希望它可以帮到你。