C# 从DataGrid获取所选行

C# 从DataGrid获取所选行,c#,wpf,datagrid,C#,Wpf,Datagrid,我正在使用WPF网格。第一列是复选框列,我的页面上有一个保存按钮。单击按钮时,我希望选中所有复选框行,在数据表中插入一个 如何做到这一点?需要此帮助吗 它使用一个decorator模式来包装一个类(可能来自像LINQ2SQL这样的ORM),使您能够对一个“选定”值进行数据绑定。保存后,您可以在装饰项目列表中查找选定项目,并仅保存这些项目 (简单示例,不符合MVVM) XAML: <Window x:Class="CheckTest.MainWindow" xmlns="ht

我正在使用WPF网格。第一列是复选框列,我的页面上有一个保存按钮。单击按钮时,我希望选中所有复选框行,在数据表中插入一个


如何做到这一点?

需要此帮助吗

它使用一个decorator模式来包装一个类(可能来自像LINQ2SQL这样的ORM),使您能够对一个“选定”值进行数据绑定。保存后,您可以在装饰项目列表中查找选定项目,并仅保存这些项目

(简单示例,不符合MVVM)

XAML:

<Window x:Class="CheckTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Foos}" Name="gridFoos" Margin="0,0,0,34">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Header="Selected" Binding="{Binding Path=Selected}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Value.Name}" />
                <DataGridTextColumn Header="Age"  Binding="{Binding Path=Value.Age}"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="423,283,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="btnSave_Click" />
    </Grid>
</Window>
namespace CheckTest
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<SelectedDecorator<Foo>> Foos { get; set; }
        public MainWindow()
        {
            var bars = new List<Foo>() { new Foo() { Name = "Bob", Age = 29 }, new Foo() { Name = "Anne", Age = 49 } };
            var selectableBars = bars.Select(_ => new SelectedDecorator<Foo>(_)).ToList();
            Foos = new ObservableCollection<SelectedDecorator<Foo>>(selectableBars);
            InitializeComponent();
            DataContext = this;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            var saveThese = Foos.ToList().Where(_ => _.Selected).Select(_=> _.Value);
            //db.BulkUpdate(saveThese); ... or some sutch;
        }
    }
    public class Foo
    {
        public string Name{get;set;}
        public int Age { get; set; }
    }
    public class SelectedDecorator<T>
    {
        public T Value { get; set; }
        public bool Selected { get; set; }
        public SelectedDecorator(T value)
        {
            Selected = false;
            this.Value = value;
        }
    }
}

代码隐藏:

<Window x:Class="CheckTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Foos}" Name="gridFoos" Margin="0,0,0,34">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Header="Selected" Binding="{Binding Path=Selected}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Value.Name}" />
                <DataGridTextColumn Header="Age"  Binding="{Binding Path=Value.Age}"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="423,283,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="btnSave_Click" />
    </Grid>
</Window>
namespace CheckTest
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<SelectedDecorator<Foo>> Foos { get; set; }
        public MainWindow()
        {
            var bars = new List<Foo>() { new Foo() { Name = "Bob", Age = 29 }, new Foo() { Name = "Anne", Age = 49 } };
            var selectableBars = bars.Select(_ => new SelectedDecorator<Foo>(_)).ToList();
            Foos = new ObservableCollection<SelectedDecorator<Foo>>(selectableBars);
            InitializeComponent();
            DataContext = this;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            var saveThese = Foos.ToList().Where(_ => _.Selected).Select(_=> _.Value);
            //db.BulkUpdate(saveThese); ... or some sutch;
        }
    }
    public class Foo
    {
        public string Name{get;set;}
        public int Age { get; set; }
    }
    public class SelectedDecorator<T>
    {
        public T Value { get; set; }
        public bool Selected { get; set; }
        public SelectedDecorator(T value)
        {
            Selected = false;
            this.Value = value;
        }
    }
}
名称空间检查测试
{
公共部分类主窗口:窗口
{
公共可观测集合Foos{get;set;}
公共主窗口()
{
var bar=new List(){new Foo(){Name=“Bob”,Age=29},new Foo(){Name=“Anne”,Age=49};
var selectablebar=bar.Select(=>newselectedDecorator()).ToList();
Foos=新的可观察集合(可选择条);
初始化组件();
DataContext=this;
}
私有void btnSave_单击(对象发送方,路由目标)
{
var savethises=Foos.ToList().Where(=>u.Selected)。Select(=>u.Value);
//db.BulkUpdate(savethis);…或一些sutch;
}
}
公开课Foo
{
公共字符串名称{get;set;}
公共整数{get;set;}
}
公共类选择除颤器
{
公共T值{get;set;}
已选择公共布尔值{get;set;}
公共选择转换器(T值)
{
所选=假;
这个。值=值;
}
}
}

能否提供一些代码作为示例?