我的数据网格包含一些文本列,所有文本列的默认值为1.另外,我使用名为“save”的按钮来保存数据网格的文本列的值。 我的要求是“保存”按钮只应在数据网格的文本列值发生变化时启用。请帮助我。
以下是我的代码: -
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="Save" 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");
}
}
答案 0 :(得分:0)
向ViewModel添加属性:
private bool _columnModified = false;
public bool columnModified{
get { return _columnModified; }
set
{
_columnModified = value;
}
}
将此添加到budgetReports setter:
columnModified = true;
在视图中设置按钮IsEnabled属性绑定到columnModified:
<Button Content="Save" IsEnabled="{Binding columnModified}"/>
保存完成后,您还必须将columnModified设置为false。
答案 1 :(得分:0)
将绑定更改为
<Binding Path="budgetReportYear1" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
现在,当每次用户在文本框中键入时,属性都会更改。在内部设置器中,启用按钮为真,即
将保存按钮的IsEnabled
绑定到bool属性并将其设置为true。
在保存按钮单击时,将属性设置为false以重置按钮状态。