1

I come from an ASP.NET background, and I've been tasked with maintaining a WPF project, so it's quite a learning curve.

I have a DataGrid with the following XAML

<DataGrid Name="StockGV" AutoGenerateColumns="False" IsReadOnly="True">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Scanned" IsReadOnly="true">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Name="StatusImage" Source="tick.png"></Image>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding PalletScanned}" Value="False">
                                <Setter TargetName="StatusImage" Property="Source" Value="cross.png"/>
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="Pallet Number" IsReadOnly="True" Binding="{Binding PalletNumber}">
            </DataGridTextColumn>
            <DataGridTextColumn Header="Quantity" IsReadOnly="True" Binding="{Binding Quantity}">
            </DataGridTextColumn>
            <DataGridTemplateColumn IsReadOnly="true">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <xctk:IntegerUpDown HorizontalAlignment="Left" Name="integerUpDown1" Maximum="{Binding Quantity}" Minimum="0" VerticalAlignment="Top" Value="{Binding Quantity, Mode=OneWay}" />
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding PalletScanned}" Value="False">
                                <Setter TargetName="integerUpDown1" Property="IsEnabled" Value="False"/>
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

The user updates the last field (the IntegerUpDown control), and then they click a 'Save' button below the grid. I need this to iterate through each row, get the value of the IntegerUpDown control and save this to the DB. If this was ASP.NET, I'd do something like this:

foreach (GridViewRow row in gv.Rows)
{
     long pk = (long)gv.DataKeys[row.RowIndex].Value;
     int value = (int)((IntegerUpDown)row.FindControl("integerUpDown1")).Value;
     //Save to DB
}

How would I do this in WPF? Note if it helps, the DataGrid is bound to an anonymous type

1 Answer 1

5

Wow that is not like its meant to be :)

You should normally bind the DataGrid to an ItemsSource of Type T. Inside the Columns you should bind to properties of your Type T.

Every change on your IntegerUpDown Control is then represented in the underlying Collection.

To access this value later in your ViewModel you can just query the Collection with Linq to get the Values.

You already got this code in your Control:

Value="{Binding Quantity, Mode=OneWay}"

So you can access the Quantity property to get the value you want.

Remember: If the User should be able to change the Quantity Property, the Binding has to be Mode=TwoWay and there needs to be a public setter for this Property too.

2
  • 1
    Don't forget the INotifyPropertyChanged.
    – Tobias
    Dec 14, 2012 at 12:00
  • Yea true, thanks for that. I totally forgot to mention it! :) Dec 14, 2012 at 15:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.