如何复制现有列并将其插入到数据网格中?
我目前有一个数据网格,其中包含每列的唯一上下文菜单。示例 xaml 如下:
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="ContextMenu" Value="{StaticResource default_columnHeaderMenu}" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Person" Binding="{Binding Path=PersonID}" CellStyle="{StaticResource CenterAlignedCellStyle}" />
....
在上下文菜单中,我有一个名为“插入列”的项目,其中包含一个列列表,如下所示:
<ContextMenu x:Key="default_columnHeaderMenu" Name="defaultColumnHeaderMenu">
<MenuItem Header="Insert Column" >
<MenuItem Header="Person ID" Click="addColumn"/>
<MenuItem Header="Health" />
<MenuItem Header="Person Description" />
<MenuItem Header="Person Age" />
.....
我的问题是,使用列标题上下文菜单,将这些列添加或复制到我的数据网格中的最简单方法是什么? (我不是在寻找一个全新的空列,而是寻找带有相同数据的已存在列的某种“副本”,等等)
使用 DataGrid.Columns,有没有办法找到索引基于标题名称的列?
感谢您的任何帮助/建议。
I currently have a datagrid that contains a unique context menu for each column.. sample xaml is as follows:
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="ContextMenu" Value="{StaticResource default_columnHeaderMenu}" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Person" Binding="{Binding Path=PersonID}" CellStyle="{StaticResource CenterAlignedCellStyle}" />
....
Inside the context menu, I have an item called "Insert column" which then contains a list of columns like so:
<ContextMenu x:Key="default_columnHeaderMenu" Name="defaultColumnHeaderMenu">
<MenuItem Header="Insert Column" >
<MenuItem Header="Person ID" Click="addColumn"/>
<MenuItem Header="Health" />
<MenuItem Header="Person Description" />
<MenuItem Header="Person Age" />
.....
MY question is, using the column header context menus, what is the easiest way to add or duplicate these columns into my data grid ? (I'm not looking for a brand new, empty column, but some kind of "copy" of an already exiting column w/ the same data, etc.)
Using DataGrid.Columns, is there a way to some how find an index of a column based on the header name ?
Thanks for any help / suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将列定义为非共享资源:
然后在菜单项事件中您可以检索它并将其添加到 DataGrid:
You could define your columns as non-shared resources:
Then in the menu item event you can retrieve it and add it to the DataGrid: