获取WPF数据网格中的选定项目 [英] Get Selected items in a WPF datagrid
问题描述
大家好,
在我的WPF应用程序中,我需要在点击数据网格外部的按钮时获取数据网格的所有选定行。我在行标题模板中使用复选框来选择或取消选择行。下面是我的datagrid xaml的小代码片段。请点击按钮,任何人都可以帮助我获取所有选定的行。我在谷歌上搜索了很多,但是所有的链接都是关于dataGrid_SelectionChanged事件中的选定项目,这很容易。
Hi all,
In my WPF application, I need to get all the selected rows of a datagrid on the click of a button which is outside the datagrid.I am using a checkbox in the row header template to select or deselect a row. Below is the small code snippet of my datagrid xaml. Please can any one help me out in fetching all the selected rows on the click of a button. I have searched a lot on google, but all the links are regarding fethcing selected items on "dataGrid_SelectionChanged" event, which is very easy.
<my:DataGrid AutoGenerateColumns="False" SelectionChanged="dataGrid_SelectionChanged" Initialized="DataGrid_Initialized" >
<my:DataGrid.RowHeaderTemplate>
<DataTemplate>
<Grid>
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:DataGridRow}}}"/>
</Grid>
</DataTemplate>
</my:DataGrid.RowHeaderTemplate>
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Width="160" Header="Media Status" >
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5,8,5,0">
<TextBlock>
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding MediaStatus}" Value="False">
<Setter Property="TextBlock.Text" Value="Not Processed" />
</DataTrigger>
<DataTrigger Binding="{Binding MediaStatus}" Value="True">
<Setter Property="TextBlock.Text" Value="Processed" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>
谢谢
Anurag
Thanks
Anurag
推荐答案
感谢所有正在阅读的人这个帖子。我自己做的。我发布下面的代码,供其他需要它的人参考。请建议是否可以对其进行任何修改。
Thanks everyone who are reading this post. I did it myself. I am posting the code below, for reference of others who need it. Please suggest if any good modification can be made to it.
private void btnProcessMedia_Click(object sender, RoutedEventArgs e)
{
if (dgProjects.SelectedItems.Count > 0)
{
for (int i = 0; i < dgProjects.SelectedItems.Count; i++)
{
System.Data.DataRowView selectedFile = (System.Data.DataRowView)dgProjects.SelectedItems[i];
string str = Convert.ToString(selectedFile.Row.ItemArray[10]);
}
}
}
Anurag
Anurag
更好的方法:
A better way:
foreach (var data in dataGridMain.SelectedItems)
{
MyObservableCollection myData = data as MyObservableCollection;
MessageBox.Show(myData.author);
}
其中MyObservableCollection是绑定到DataGrid的数据类(在本例中为dataGridMain)。例如,这个集合有一个作者字符串。
Where "MyObservableCollection" is your data class that is bound to the DataGrid (in this case dataGridMain). And this collection has an author string for example.
请阅读此链接。
DataGrid中的MultiItemSelect行为 [ ^ ]
Please read this link.
MultiItemSelect Behaviour in DataGrid [^]
这篇关于获取WPF数据网格中的选定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!