我有这个 DataGridTextColumn,我试图在上面放置一个 IMultiValueConverter。
在转换器中,当我执行以下操作时 GenericDataGrid 的绑定:
Console.WriteLine(values[1].GetType());
返回为MS.Internal.NamedObject
我该如何解决这个问题,以便它为我提供对 DataGrid 的引用?
<DataGrid Name="GenericDataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding UserCollection}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Job_Title}">
<DataGridTextColumn.Header>
<MultiBinding Converter="{StaticResource metaDataDisplayNameConverter}">
<MultiBinding.Bindings>
<Binding RelativeSource="{x:Static RelativeSource.Self}"/>
<Binding ElementName="GenericDataGrid" />
</MultiBinding.Bindings>
</MultiBinding>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
[编辑]
这是我在制作绑定代理后所拥有的,但这与Path="Data"
. 输入后,我得到Object reference not set to an instance of an object
.
你能看出代理绑定有什么问题吗?
<DataGrid Name="GenericDataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding UserCollection}">
<DataGrid.Resources>
<proxy:FreezableBindingProxy x:Key="proxy" Data="{Binding ElementName=GenericDataGrid}" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Job_Title}">
<DataGridTextColumn.Header>
<MultiBinding Converter="{StaticResource metaDataDisplayNameConverter}">
<MultiBinding.Bindings>
<Binding RelativeSource="{x:Static RelativeSource.Self}"/>
<Binding Source="{StaticResource proxy}" Path="Data" />
</MultiBinding.Bindings>
</MultiBinding>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
[编辑 2]
另一个相关的问题。在转换器中,当我尝试获取 DataGrid 的 ItemsSource 时,它会抛出一堆错误。知道为什么会这样吗?
我唯一追求的是集合类型,也许有更好的方法。
实际上,我收回了它是重要的集合中的单一项目类型。
public object Convert(object[] values, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string DisplayName = "Col";
object ColumnObject = values[0];
DataGrid dg = (DataGrid)values[1];
string columnPropertyName = parameter.ToString();
var collection = dg.ItemsSource; //*****Fails here*****
Type collectionType = collection.GetType();
Type itemType = collectionType.GetGenericArguments().Single();
PropertyInfo prop = itemType.GetProperties().SingleOrDefault(p => p.Name.Equals(columnPropertyName));
//....Gets properties display name attribute here.
return DisplayName;
}
错误:
A first chance exception of type 'System.NullReferenceException' occurred in UserManagement.dll
A first chance exception of type 'System.NullReferenceException' occurred in System.Xaml.dll
A first chance exception of type 'System.NullReferenceException' occurred in PresentationFramework.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll