如何在XAML中正确使用DataGrid / DataGridTextColumn的SortMemberPath作为WPF中的嵌套对象层次结构? [英] how to use SortMemberPath of DataGrid / DataGridTextColumn in XAML correctly for nested object hierarchy in WPF?

查看:89
本文介绍了如何在XAML中正确使用DataGrid / DataGridTextColumn的SortMemberPath作为WPF中的嵌套对象层次结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WPF DataGrid来显示SQL数据库表的内容。

i am trying to use a WPF DataGrid to show the content of an SQL database table.

我在表中显示的一些数据包含嵌套的对象数据。数据绑定工作正常并正确显示数据。

some data i show in the table contains nested object data. the binding of the data works perfectly and shows the data correctly.

但如果我尝试对列进行排序,它将抛出NullReference异常,因为我尝试对列进行排序对嵌套的对象数据有所了解。

i也试图明确地给出SortMemberPath,但没有成功。当我给出一个看起来像绑定路径的SortMemberPath时,我也得到一个NullReference异常。

but if i try to sort a column it will throw an NullReference exception, as soon i try to sort a column where the column has bint to nested object data.
i also tried to explicitely give the SortMemberPath, but without success. when i give a SortMemberPath that looks like the binding path, i also get a NullReference exception.

有人可以告诉我,我在做什么错误以及我必须如何正确地做到这一点。

Can somebody show me, what i am doing wrong and how i have to do it correctly.

我使用.NET Framework 4.6.1,

i use .NET Framework 4.6.1,

访问数据库我使用SQL到LINQ

to access to the database i use SQL to LINQ

XAML文件的conetent如下所示:

the conetent of the XAML file looks like:

<Window x:Class="WpfApp_Test_SortMember.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="Test - MainWindow"
        Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" Text="Normal definition"/>
        <DataGrid Grid.Row="1" Grid.Column="0" ItemsSource="{Binding C_Items, Mode=OneWay}" IsReadOnly="True" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="C.Id" Binding="{Binding Id}"/>
                <DataGridTextColumn Header="C.Name" Binding="{Binding Name}"/>
                <DataGridTextColumn Header="C.B.Id" Binding="{Binding B_Item.Id}"/>
                <DataGridTextColumn Header="C.B.Name" Binding="{Binding B_Item.Name}"/>
                <DataGridTextColumn Header="C.B.A.Id" Binding="{Binding B_Item.A_Item.Id}"/>
                <DataGridTextColumn Header="C.B.A.Name" Binding="{Binding B_Item.A_Item.Name}"/>
            </DataGrid.Columns>
        </DataGrid>
            
        <TextBlock Grid.Row="0" Grid.Column="1" Text="With SortMember given"/>
        <DataGrid Grid.Row="1" Grid.Column="1" ItemsSource="{Binding C_Items, Mode=OneWay}" IsReadOnly="True" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="C.Id" Binding="{Binding Id}" SortMemberPath="Id"/>
                <DataGridTextColumn Header="C.Name" Binding="{Binding Name}" SortMemberPath="Name"/>
                <DataGridTextColumn Header="C.B.Id" Binding="{Binding B_Item.Id}" SortMemberPath="B_Item.Id"/>
                <DataGridTextColumn Header="C.B.Name" Binding="{Binding B_Item.Name}" SortMemberPath="B_Item.Name"/>
                <DataGridTextColumn Header="C.B.A.Id" Binding="{Binding B_Item.A_Item.Id}" SortMemberPath="B_Item.A_Item.Id"/>
                <DataGridTextColumn Header="C.B.A.Name" Binding="{Binding B_Item.A_Item.Name}" SortMemberPath="B_Item.A_Item.Name"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

及其背后的代码是:

using System.Windows;

namespace WpfApp_Test_SortMember
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = new TestDataClassesDataContext();
        }
    }
}

,窗口看起来像

...我得到一个例外。列C.Id和C.Name将被排序而没有异常,因为它的数据不是嵌套的。

as soon i click to the column header or C.B. ... or C.B.A. ... i get an exception. columns C.Id and C.Name will be sorted without an exception, because its data are not nested.

推荐答案

据我所知,如果某个属性有点,则Binding将失败。

As far as I know, if a property has dot , Binding will failed.

https://stackoverflow.com/questions/6868088/dot-in -binding-path-in-wpf-cause-issue / 6869637

虽然使用方括号会成功绑定,但排序会失败。

Although use square bracket would binding successful, but sorting would failed.

所以你不应该在房产中加点。

So you should not have dot in property.

此致,

Bob


这篇关于如何在XAML中正确使用DataGrid / DataGridTextColumn的SortMemberPath作为WPF中的嵌套对象层次结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆