【问题标题】:Navigating between data in DataGrid WPF XAML在 DataGrid WPF XAML 中的数据之间导航
【发布时间】:2014-02-11 07:54:22
【问题描述】:

我有代表我的程序中用户可以访问的模块的复选框。

您可以从 DataGrid 中看到,第四列中的模块是从“DataContext.ModuleCollection”创建的,而 DataGrid 的行是从 UserCollection 创建的。我希望 User_ID 成为每个复选框的内容。这将设置为我在底部尝试过的复选框样式,但没有取得多大成功。

我的问题是,复选框位于模块级别(在数据中),我需要想办法回到用户级别以获取 User_ID。

User_ID 是行数据集的一部分,例如用户记录。

我如何获得下面样式的 User_ID 是个问题?

下面是我的数据网格:

<DataGrid  ItemsSource="{Binding UserCollection}" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"/>
        <DataGridTextColumn Header="Job Title" Binding="{Binding Path=Job_Title}"/>
        <DataGridTextColumn Header="Company" Binding="{Binding Path=Company}"/>
        <DataGridTemplateColumn CanUserReorder="True">
            <DataGridTemplateColumn.Header>
                <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ModuleCollection}" >
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" Background="Transparent" CanVerticallyScroll="False" CanHorizontallyScroll="False"></StackPanel>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Style="{StaticResource ListViewItemRotatedText}" Text="{Binding ModuleName}" >
                            </TextBlock>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataGridTemplateColumn.Header>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ModuleCollection}" >
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal" Background="Transparent" CanVerticallyScroll="False" CanHorizontallyScroll="False"></StackPanel>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <CheckBox Style="{StaticResource ListViewItemCheckbox}" />
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

以下是我的检查方式:

<Style x:Key="ListViewItemCheckbox" TargetType="CheckBox">
        <Setter Property="Content" Value="{Binding Path=User_ID, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}"/>
</Style>

【问题讨论】:

  • 我希望 User_ID 成为每个复选框的内容。您希望每一行中的相同 User_ID 值出现在 all 中那一行有五个Checkboxes?
  • 是的,它可能看起来很奇怪,但我只是想弄清楚如何获得它的参考。我最终会将其发送到与复选框相同级别的转换器。
  • 这是我第四次改写这个问题,非常感谢您的帮助!
  • 我知道...它一直难以理解...现在回答。给我5个

标签: wpf xaml wpfdatagrid


【解决方案1】:

您当前的Binding Path 将不起作用,因为您正尝试从DataGridRow 访问User_ID 属性,当然,它没有...你应该在 Visual Studio 的输出窗口中出现错误...密切注意这些错误,因为它们将帮助您解决问题。

那么Binding Path可以你用什么?好吧,就我们必须使用RelativeSource Binding这一事实而言,你是对的……试试这个吧:

<Style x:Key="ListViewItemCheckbox" TargetType="CheckBox">
    <Setter Property="Content" Value="{Binding Path=DataContext.User_ID, 
        RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" />
</Style>

我们试图在这里做的是将数据绑定到User_ID 值,该值应该在对象集中找到,作为DataGridRowDataContext


更新>>>

啊,是的,我的想法无论如何都行不通,因为我试图绑定到整个集合,而您需要绑定到当前项目。但是,由于您组织(或杂乱)数据模型的方式,您只会遇到这些可怕的问题。这些CheckBoxes 应该是来自一个或多个属性的数据,这些属性实际上在您的User class 中,而不是在这个单独的集合中。在您遇到更严重的问题之前,我会立即修复您的模型。


更新 2 >>>

您现在想知道如何绑定到整个数据对象而不是该对象的属性。有关更多信息,请参阅 MSDN 上的 PropertyPath XAML Syntax 页面以供将来参考。但是,您只需要考虑一下...如果DataContext.User_ID 为您获取User 对象的User_ID 属性值, 认为会返回整个User 对象?没错...只需删除该属性:

<Style x:Key="ListViewItemCheckbox" TargetType="CheckBox">
    <Setter Property="Content" Value="{Binding Path=DataContext, 
        RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" />
</Style>

【讨论】:

  • 我有点看到你要去哪里但是它在输出窗口中返回以下内容: System.Windows.Data 警告:40:BindingExpression 路径错误:在“对象”上找不到“用户 ID”属性''模块' (HashCode=62906887)'。绑定表达式:路径=用户 ID; DataItem='模块' (HashCode=62906887);目标元素是 'CheckBox' (Name='');目标属性是“NoTarget”(类型“对象”)在 UserManagement.dll 中发生“System.InvalidCastException”类型的第一次机会异常
  • 我明白你的意思,为每个用户创建一个模块集合?如果不是这个确切的示例,仍然必须有一种方法可以从复选框中获取 User_ID。没关系,刚刚找到它,将 x:Type DataGrid 换成 DataGridRow 并且它已经完成了。感谢您的耐心等待,接下来我会将您对数据模型的建议付诸实施
  • 另一个相关问题。如果我想从同一个位置绑定到 User 对象,而不是它的属性之一,你会怎么做?
猜你喜欢
  • 2020-05-21
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多