简体   繁体   中英

Checkbox event that fires AFTER the value has changed

I'm creating my very first wpf application. At this moment I need an event that fires AFTER the value of a checkbox has changed, so checked and unchecked are out of the picture :-(

I have a datagrid, binded to an observable collection. In this datagrid I have a column with checkboxes (binded to a property in the observ. col.). A textbox on the form shows the 'total value' that is the sum of the values of all checked items. So when a checkbox is checked/unchecked I need to recalculate the total value. To do this I loop over the items of the observ. col. However, when I attach the events 'checked' and 'unchecked'. The total value gets calculated first. Then the check-value is changed.

Is there a way to have the check value changed and after that fire an event?

Thx,

Jan

<DataGrid AutoGenerateColumns="False" Height="305" Margin="105,137,0,0" Name="GrdReceivings" VerticalAlignment="Top" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" ItemsSource="{Binding}" HorizontalAlignment="Left" Width="850" SelectionMode="Single" CanUserAddRows="False" CanUserDeleteRows="False" SelectedCellsChanged="GrdReceivings_SelectedCellsChanged" MouseDoubleClick="GrdReceivings_MouseDoubleClick" IsEnabled="True">
    <DataGrid.Columns>
            <DataGridCheckBoxColumn MinWidth="40" Binding="{Binding Path=Selected}" >
                <DataGridCheckBoxColumn.CellStyle>
                    <Style>
                        <EventSetter Event="CheckBox.Checked" Handler="OnCheck"/>
                        <EventSetter Event="CheckBox.Unchecked" Handler="OnUncheck"/>
                    </Style>
                </DataGridCheckBoxColumn.CellStyle>
            </DataGridCheckBoxColumn>
...

You're doing this the wrong way around for WPF. Instead of looking at the UI controls, look at the data. You said that you were binding the Checkbox controls to a property of the ObservableCollection ... I'm guessing that you meant a property of the object inside the ObservableCollection and not the collection itself.

So in your view model or code behind, update your total value when the property that is bound to the Checkbox control is changed. This way, it will have the updated value every time.

You have to set UpdateSourceTrigger=PropertyChanged in order to get the binding updated immediately:

<DataGridCheckBoxColumn 
    Binding="{Binding Path=Selected, UpdateSourceTrigger=PropertyChanged}"/>

Thx to Sheridan & Clemens, it did the trick. To formulate the good answer:

<DataGrid AutoGenerateColumns="False" Height="305" Margin="105,137,0,0" Name="GrdReceivings" VerticalAlignment="Top" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" ItemsSource="{Binding}" HorizontalAlignment="Left" Width="850" SelectionMode="Single" CanUserAddRows="False" CanUserDeleteRows="False" SelectedCellsChanged="GrdReceivings_SelectedCellsChanged" MouseDoubleClick="GrdReceivings_MouseDoubleClick" IsEnabled="True">
    <DataGrid.Columns>
            <DataGridCheckBoxColumn MinWidth="40" Binding="{Binding Path=Selected, UpdateSourceTrigger=PropertyChanged}" />

then, in the observable collection, on the setter, calculate the value.

SOLVED!!!

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