DataGrid 填充 TreeView 的 SelectedItemChanged 上的空白行

发布于 2024-10-11 14:48:44 字数 1653 浏览 1 评论 0原文

当我的 DataGrid 填充 TreeView 的 SelectedItemChanged 事件时,它会找到对象并相应地创建行,但填充的行没有文本或只是空白。所以我知道它正在找到我的对象,但它没有正确显示它们。有人看到我在哪里犯了错误或建议进行任何更改或修复吗?先感谢您!

这是设置 DataGrid 的 ItemsSource 的 CSharp 代码(我使用 .dbml 和带有 Lambda 表达式的 LINQ):

dgSystemSettings.ItemsSource = (tvSystemConfiguration.SelectedItem as SYSTEM_SETTINGS_GROUP)
                               .SYSTEM_SETTINGS_NAMEs
                               .Join(ssdc.SYSTEM_SETTINGS_VALUEs, x => x.SSN_ID, 
                                     y => y.SSV_SSN_ID, 
                                     (x, y) => new { SYSTEM_SETTINGS_NAME = x, 
                                     SYSTEM_SETTINGS_VALUE = y });

这是 .xaml:

    <DataGrid Name="dgSystemSettings" 
              AutoGenerateColumns="False" 
              Height="447" Width="513" 
              DockPanel.Dock="Right" 
              ItemsSource="{Binding}" 
              VerticalAlignment="Top" 
              Margin="10,10,0,0">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="colDisplayName" 
                                Header="Name" 
                                Binding="{Binding SSN_DISPLAY_NAME}"></DataGridTextColumn>
            <DataGridTextColumn x:Name="colValue" 
                                Header="Value" 
                                Binding="{Binding SSV_VALUE}"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

SSN_DISPLAY_NAME 是 SYSTEM_SETTINGS_NAMES 中的一列 SSV_VALUE 是 SYSTEM_SETTINGS_VALUES 中的一列,

我在 lambda 表达式中加入了表,因此我只能在 DataGrid 中显示这两列。

When my DataGrid populates on the TreeView's SelectedItemChanged event it finds the objects and creates the rows accordingly but the rows populate with no text or are just blank. So I know it is finding my objects but it is not displaying them properly. Does anyone see where I made an error or suggest any changes or fixes? Thank you in advance!

Here is the CSharp code that is setting the DataGrid's ItemsSource (I am using .dbml and LINQ with Lambda expressions):

dgSystemSettings.ItemsSource = (tvSystemConfiguration.SelectedItem as SYSTEM_SETTINGS_GROUP)
                               .SYSTEM_SETTINGS_NAMEs
                               .Join(ssdc.SYSTEM_SETTINGS_VALUEs, x => x.SSN_ID, 
                                     y => y.SSV_SSN_ID, 
                                     (x, y) => new { SYSTEM_SETTINGS_NAME = x, 
                                     SYSTEM_SETTINGS_VALUE = y });

And here is the .xaml:

    <DataGrid Name="dgSystemSettings" 
              AutoGenerateColumns="False" 
              Height="447" Width="513" 
              DockPanel.Dock="Right" 
              ItemsSource="{Binding}" 
              VerticalAlignment="Top" 
              Margin="10,10,0,0">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="colDisplayName" 
                                Header="Name" 
                                Binding="{Binding SSN_DISPLAY_NAME}"></DataGridTextColumn>
            <DataGridTextColumn x:Name="colValue" 
                                Header="Value" 
                                Binding="{Binding SSV_VALUE}"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

SSN_DISPLAY_NAME is a column in SYSTEM_SETTINGS_NAMES
SSV_VALUE is a column in SYSTEM_SETTINGS_VALUES

I joined the tables in my lambda expression so I can display only these two columns in my DataGrid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱倦旧时光 2024-10-18 14:48:44

我相信您可能希望将 DataGridTextColumn 的文本更改为以下内容:

<DataGrid Name="dgSystemSettings" AutoGenerateColumns="False" Height="447" Width="513" DockPanel.Dock="Right" ItemsSource="{Binding}" VerticalAlignment="Top" Margin="10,10,0,0">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="colDisplayName" Header="Name" Binding="{Binding SYSTEM_SETTING_NAME.SSN_DISPLAY_NAME}"></DataGridTextColumn>
        <DataGridTextColumn x:Name="colValue" Header="Value" Binding="{Binding SYSTEM_SETTING_VALUE.SSV_VALUE}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

由于您的行填充了正确数量的项目,因此我认为这一定是一个绑定问题。当您设置 DataGrid 的 ItemsSource 时,它​​会查找对象的视图集合。您的数据都在那里,您只需要在绑定中包含表名称即可。代码中的每个项目都包含表的两个对象,并且表包含您要查找的属性。

I believe that you may want to change the text of the DataGridTextColumn to the following:

<DataGrid Name="dgSystemSettings" AutoGenerateColumns="False" Height="447" Width="513" DockPanel.Dock="Right" ItemsSource="{Binding}" VerticalAlignment="Top" Margin="10,10,0,0">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="colDisplayName" Header="Name" Binding="{Binding SYSTEM_SETTING_NAME.SSN_DISPLAY_NAME}"></DataGridTextColumn>
        <DataGridTextColumn x:Name="colValue" Header="Value" Binding="{Binding SYSTEM_SETTING_VALUE.SSV_VALUE}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

Since your rows were populating the correct number of items, I figured it had to be a binding issue. When you set the ItemsSource of the DataGrid, it looks for a View Collection of the object. Your data was all there, you just needed to include the table names in the bindings. Each item in your code contained two objects for the tables and the tables contained the properties you were looking for.

腻橙味 2024-10-18 14:48:44

我在 xaml 中将 AutoGenerateColumns 设置为 False。将其设置为 True 修复了此问题。

I had AutoGenerateColumns set to False in my xaml. Setting it to True fixed this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文