简体   繁体   中英

WPF DataGrid - Iterating through rows and accessing control in template column

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM