wpf 如何使用复选框创建 DatagridTemplateColumn 并绑定到数据源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27483746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:47:35  来源:igfitidea点击:

How to create a DatagridTemplateColumn with checkbox and binding to a datasouce?

c#wpfxamlcheckbox

提问by Sarah

Apologies in advance if I'm overlooking something - I'm still finding my feet working with Xaml rather than windows forms.

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

I'm trying to bind a data source to a DataGrid where one of the columns is a checkbox. My original solution worked fine for this, but required the user to double click the checkbox:

我正在尝试将数据源绑定到 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>

The new solution for single click checkboxes is displayed below, but doesn't work (error is shown on the Binding="{Binding Generate}"):

单击复选框的新解决方案如下所示,但不起作用(错误显示在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>

For reference, the code used to set the source is:

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

InvoiceDG.ItemsSource = recordCollection;

Where recordCollection is a list of:

其中 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;
    }
}

回答by Anka

Try this:

尝试这个:

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