Checkbox event that fires AFTER the value has changed

ぐ巨炮叔叔 提交于 2020-01-06 02:26:11

问题


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

回答1:


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.




回答2:


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

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



回答3:


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



来源:https://stackoverflow.com/questions/18013426/checkbox-event-that-fires-after-the-value-has-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!