4

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

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:

<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}"):

<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:

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;
    }
}
2
  • Which flavor of XAML? Are you writing a desktop or a phone app? WPF, Silverlight, or Windows Store app (WinRT)? Commented Dec 15, 2014 at 13:02
  • desktop app, WPF (visual studio 2012) Commented Dec 15, 2014 at 13:09

1 Answer 1

8

Try this:

<DataGridTemplateColumn Header="Generate" Width="60">
     <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
                <CheckBox IsChecked="{Binding Generate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
           </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Sign up to request clarification or add additional context in comments.

4 Comments

This code compiles, and creates a single click checkbox, but doesn't reflect changes back to the data source sadly :( I've updated original post to show where data source is set.
Use UpdateSourceTrigger: <CheckBox IsChecked="{Binding Generate, UpdateSourceTrigger=PropertyChanged}"/>
Shouldn't it be "{Binding Generate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
OMG this just ended hours of mindless trial and error for me. Thank you! But why is the UpdateSourceTrigger=PropertyChanged necessary? It's not necessary for normal checkboxes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.