在WPF中,调用datagrid.CancelEdit()后不会回滚DataGridTextColumn的值

时间:2016-07-15 10:10:04

标签: wpfdatagrid wpd

我有一个包含两个文本列的数据网格。允许用户修改默认值(即1)。在按钮上单击我显示确认消息如果用户按是,数据网格文本列的值应保存为新值,如果为否,网格文本列的值应滚动/重置为默认值或上一个值。问题在于,当用户单击确认消息中的“否”按钮时,数据网格文本列值不会回滚或重置为默认值。请帮我。                    视图:-                       

                  x:Name="datagrid_range_targets" local:SetMinWidthToAutoAttachedBehaviour.SetMinWidthToAuto="true"
                  ItemsSource="{Binding RangeData, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" 
                  Margin="0,5,5,0"  Loaded="OnUserControlLoaded" Unloaded="OnUserControlUnloaded" Style="{StaticResource style_data_grid}">
            <DataGrid.Resources>
                <Style x:Key="errorStyle" TargetType="{x:Type TextBox}">
                    <Setter Property="Padding" Value="-2"/>
                    <Style.Triggers>
                        <Trigger Property="Validation.HasError" Value="True">
                            <Setter Property="Background" Value="Red"/>
                            <Setter Property="ToolTip" 
                             Value="{Binding RelativeSource={RelativeSource Self},
                             Path=(Validation.Errors)[0].ErrorContent}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>

            <DataGrid.Columns>
                <DataGridTextColumn x:Name="col_range_targets_range"
                                    Width="Auto"
                                    Binding="{Binding Name}"
                                    Header="Range" IsReadOnly="True" />

                <DataGridTextColumn x:Name="col_budget_report_year1"
                                    Width="Auto" 
                                    Header="Year 01" EditingElementStyle="{StaticResource errorStyle}">
                    <DataGridTextColumn.Binding>
                        <Binding Path="budgetReportYear1" Mode="TwoWay"
                                >
                            <Binding.ValidationRules>
                                <local:GreaterThanOrEqualLessThanOrEqual Max="99"
                                                                        Min=".0000001" />
                            </Binding.ValidationRules>
                        </Binding>
                    </DataGridTextColumn.Binding>
                </DataGridTextColumn>
                <DataGridTextColumn x:Name="col_budget_report_year2"
                                    Width="Auto" 
                                    Header="Year 02" EditingElementStyle="{StaticResource errorStyle}">
                    <DataGridTextColumn.Binding>
                        <Binding Path="budgetReportYear2" Mode="TwoWay"
                                 UpdateSourceTrigger="Default">
                            <Binding.ValidationRules>
                                <local:GreaterThanOrEqualLessThanOrEqual Max="99"
                                                                         Min="0.0000001" />
                            </Binding.ValidationRules>
                        </Binding>
                    </DataGridTextColumn.Binding>
                </DataGridTextColumn>
 <Button x:Name="btn_Set_mulutiplier"  Click="btn_Set_mulutiplier_Click" Content="Set multiplier" 
                                               Style="{StaticResource button_SetMultiplier}"  />
               View Model-
                    private double _budgetReportYear1 = 1;
    private double _budgetReportYear2 = 1; public double budgetReportYear1
    {
        get { return _budgetReportYear1; }
        set
        {
            _budgetReportYear1 = value;
            Onchanged("budgetReportYear1");
        }
    }


    public double budgetReportYear2
    {
        get { return _budgetReportYear2; }
        set
        {
            _budgetReportYear2 = value;
            Onchanged("budgetReportYear2");
        }
    }
     Code Behind:-
               private void btn_Set_mulutiplier_Click(object sender, RoutedEventArgs e)
    {
        string message = "Do you want to set multiplier?";
        string caption = "Confirmantion";
        MessageBoxButton buttons = MessageBoxButton.YesNo;
        MessageBoxImage icon = MessageBoxImage.Question;
        if (MessageBox.Show(message, caption, buttons, icon) == MessageBoxResult.Yes)
        {
            datagrid_range_targets.CommitEdit();
        }
        else
        {

             datagrid_range_targets.CancelEdit();


        } 

    }code here`

0 个答案:

没有答案