如何创建带有复选框和绑定到数据源的数据网格模板列

本文关键字:数据 数据源 数据网 网格 绑定 何创建 创建 复选框 | 更新日期: 2023-09-27 18:34:22

如果我忽略了某些内容,请提前道歉 - 我仍然发现我的脚在使用 Xaml 而不是 Windows 窗体。

我正在尝试将数据源绑定到其中一列是复选框的 DataGrid。我最初的解决方案对此工作正常,但需要用户双击复选框:

<Window x:Class="ExecBoxInvoices.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="507" Width="676">
<Grid Margin="0,0,0,51">
    <DataGrid x:Name="InvoiceDG" HorizontalAlignment="Left" Height="165" Margin="134,251,0,0" VerticalAlignment="Top" Width="400" ItemsSource="{Binding}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Header="Generate" Binding="{Binding Generate}" Width="80"/>
            <DataGridTextColumn Header="Table_Number" Binding="{Binding Table_Number}" Width="120"/>
            <DataGridTextColumn Header="Transaction_Date" Binding="{Binding Transaction_Date}" Width="175"/>
            <DataGridTextColumn Header="Transaction_ID" Visibility="Hidden" Binding="{Binding Transaction_ID}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

下面显示了单击复选框的新解决方案,但不起作用(错误显示在 Binding="{Binding Generate}" 上):

<Window x:Class="ExecBoxInvoices.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="507" Width="676">
<Grid Margin="0,0,0,51">
    <DataGrid x:Name="InvoiceDG" HorizontalAlignment="Left" Height="165" Margin="134,251,0,0" VerticalAlignment="Top" Width="400" ItemsSource="{Binding}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Generate" Width="60" Binding="{Binding Generate}">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="Table_Number" Binding="{Binding Table_Number}" Width="120"/>
            <DataGridTextColumn Header="Transaction_Date" Binding="{Binding Transaction_Date}" Width="175"/>
            <DataGridTextColumn Header="Transaction_ID" Visibility="Hidden" Binding="{Binding Transaction_ID}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

作为参考,用于设置源的代码为:

InvoiceDG.ItemsSource = recordCollection;

其中记录集合是以下各项的列表:

class InvoiceRow
{
    public bool Generate { get; set; }
    public string Table_Number { get; set; }
    public string Transaction_Date { get; set; }
    public string Transaction_ID { get; set; }
    public InvoiceRow(bool generate, string table_Number, string transaction_Date, string transaction_ID)
    {
        this.Generate = generate;
        this.Table_Number = table_Number;
        this.Transaction_Date = transaction_Date;
        this.Transaction_ID = transaction_ID;
    }
}

如何创建带有复选框和绑定到数据源的数据网格模板列

试试这个:

<DataGridTemplateColumn Header="Generate" Width="60">
     <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
                <CheckBox IsChecked="{Binding Generate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
           </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>