问题
I have a Data Grid containing two text columns. A user is allowed to modify the default value(which is 1). on Button click i am showing a confirmation message if user press yes ,data grid text column's value should be saved with new values and in case of No , Grid text column's value should be rolled/reset back to default or previous value. The issue lies,when a user clicks on No button from confirmation message, Data grid text column values are not rolled back or resetting to default . Please help me. View:-
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`
来源:https://stackoverflow.com/questions/38393449/in-wpf-datagridtextcolumns-value-is-not-rolled-back-after-calling-datagrid-can