Wpf 为DatGrid中的每个项调用转换器
我有绑定到ObservableCollection的DataGrid。我需要将每个项目从ObservableCollection提供的实体转换为我的自定义类型“动态”。为了实现这种行为,我添加了以下XAML标记:Wpf 为DatGrid中的每个项调用转换器,wpf,datagrid,observablecollection,Wpf,Datagrid,Observablecollection,我有绑定到ObservableCollection的DataGrid。我需要将每个项目从ObservableCollection提供的实体转换为我的自定义类型“动态”。为了实现这种行为,我添加了以下XAML标记: <DataGrid x:Name="CustomGrid" ItemsSource="{Binding CollectionView}" > <DataGrid.ItemTemplate>
<DataGrid x:Name="CustomGrid" ItemsSource="{Binding CollectionView}" >
<DataGrid.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding Converter={StaticResource CustomConverter}}" />
</DataTemplate>
</DataGrid.ItemTemplate>
</DataGrid>
在上面的示例集合中,View是ObservableCollection,CustomConverter是我自己开发的转换器
通过上面的代码,我想用CustomConverter将CollectionView提供的每个实体转换为另一种类型
但是,不幸的是,代码不起作用。我在CustomConverter中设置了brekpoint,但它从未调用。
是否存在转换值的方法?
谢谢您可能需要使用
或
另一个选项是为每个项目创建自定义样式。假设每行具有相同的列集
<DataGrid x:Name="CustomGrid" ItemsSource="{Binding CollectionView}" AutoGenerateColumns="True" >
<DataGrid.ItemContainerStyleSelector>
<l:DataGridItemItemStyleSelector />
</DataGrid.ItemContainerStyleSelector>
</DataGrid>
希望这有帮助
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
public class DataGridItemItemStyleSelector : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
Employee targetIem = item as Employee;
if (targetIem != null)
{
if (targetIem.Id == 0)
{
return (Style)Application.Current.FindResource("ResourceNameDefined");
}
else
{
return base.SelectStyle(item, container);
}
}
else
{
return base.SelectStyle(item, container);
}
}
}