WPF DataGrid - 将 DataContext 分配给行
我遇到的情况是 DataGrid 的 ItemsSource 属性是整数数组。这些整数是相应字典的键。我需要在列中显示的是字段,它们是字典值对象的成员。
<DataGrid ItemsSource="{Binding Path=thisDataContext, ElementName=control, Mode=OneWay}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="0.3*" Binding="{Binding Converter={StaticResource IntToPersonConverter}, Mode=OneWay }" />
<DataGridTextColumn Header="Department" Width="0.3*" Binding="{Binding Path=DepartmentIDX, Converter={StaticResource DepartmentConverter}, Mode=OneWay }" />
<DataGridTextColumn Header="Position" Width="0.3*" Binding="{Binding Path=EmployeeTypeIDX, Converter={StaticResource EmployeeTypeConverter}, Mode=OneWay }" />
</DataGrid.Columns>
</DataGrid>
我正在使用的类 Person 的对象具有属性 - 部门和职位 ID,因此我实现了多个转换器 - 人员到部门名称和人员到职位名称。我不知道如何将它们全部结合起来。我认为如果我可以将 DataContext 属性设置为 DataGrid 行,这将是可能的。所以我可以将 Person 对象传递给 Converter。
I have a situation where ItemsSource property of a DataGrid is an array of integers. These integers are the keys to a corresponding dictionary. What I need to display in columns are the fields, that are members of the dictionary values objects.
<DataGrid ItemsSource="{Binding Path=thisDataContext, ElementName=control, Mode=OneWay}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="0.3*" Binding="{Binding Converter={StaticResource IntToPersonConverter}, Mode=OneWay }" />
<DataGridTextColumn Header="Department" Width="0.3*" Binding="{Binding Path=DepartmentIDX, Converter={StaticResource DepartmentConverter}, Mode=OneWay }" />
<DataGridTextColumn Header="Position" Width="0.3*" Binding="{Binding Path=EmployeeTypeIDX, Converter={StaticResource EmployeeTypeConverter}, Mode=OneWay }" />
</DataGrid.Columns>
</DataGrid>
The object of a class Person I'm working with have Properties - Department and Position ids, so I've implemented multiple Converters - Person to Department name and Person to Postition name. I have no idea how to combine all of them. I thought that would be possible if I could set DataContext properties to the DataGrid Rows. So I could pass a Person object to a Convertor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 items 集合是字典中的键列表,则必须编写一个转换器,将第一个字典键转换为字典值,即将 Person 对象和第二个 Person 对象转换为 Person 属性值。为了实现这一点,转换器必须能够访问字典。幸运的是,有一种方法可以将附加参数传递给 WPF 值转换器,即 Convert 和 ConvertBack 方法中的转换器参数。在这里,您可以传递字典对象,您的 Convert 方法可能如下所示:
传递转换器参数是由 ConverterParameter 绑定的属性。
If the items collection is a list of keys into a dictionary, you would have to write a converter that converts first dictionary keys to dictionary values, i.e. Person objects and second Person objects to Person property values. To achieve this, the converter must have access to the dictionary. Fortunately there is a way to pass an additional parameter to a WPF value converter, namely the converter parameter in the Convert and ConvertBack methods. Here you could pass the dictionary object and your Convert method could look like this:
Passing the converter parameter is done by the ConverterParameter property of the binding.
仅仅为了证明它们有效而使用 WPF future(例如绑定转换器等)没有多大意义 - 它们确实有效。对转换器的调用数量将是巨大的,这至少不优雅并且很容易避免。我建议您在设置网格的 ItemsSource 之前预处理您的集合。
我个人会使用 LINQ 投影,并将字典建立为类的静态成员。请给我们留言,我会发布一个示例。
更新 1 - 示例代码
There's no much point in exercising WPF futures such as binding converters etc just for the sake of proving that they work - they do. The number of calls to converter is going to be massive, which isn't elegant at least and is easily avoidable. I'd suggest that you pre-process your collection before setting grid's ItemsSource.
I would personally use LINQ projectsions, and establish the dictionaries as static members of your class. Give us a shout and I'll post a sample.
Update 1 - Sample Code