CodeGo.net>如何创建一个DatagridTemplateColumn复选框,并绑定到DataSouce?

如果我忽略了某些内容,请提前道歉-我仍然发现我的脚在使用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;

其中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>
上一篇:c#-从DataTemplate设置的自定义DependencyProperty


下一篇:CodeGo.net>如何正确地通知数据绑定控件复合属性具有新值?