<TextBox Grid.Row="1" Grid.Column="2" Width="50" Text="{Binding Size}"></TextBox>
<ComboBox Grid.Row="2" Grid.Column="1" ItemsSource="{Binding MyList, Mode=OneTime}"
SelectedValue ="{Binding ListSelectedItem, Mode=TwoWay}"></ComboBox>
<Button Grid.Row="3" Grid.Column="2" Content="Save" Command="{Binding SaveCommand}" IsEnabled="False"/>
ViewModel,
public ICommand SaveCommand { get; }
public ConfigurationViewModel()
{
SaveCommand = new RelayCommand(SaveCommandExecute);
}
public int Size
{
get
{
return _size;
}
set
{
_size = value;
OnPropertyChanged();
}
}
private void SaveCommandExecute()
{
// save logic
}
可以保存在文本框中输入的数据和在组合框中选择的值。默认情况下,保存按钮应该被禁用,如果文本框/组合框有任何变化,则启用保存按钮。
如果用户恢复到文本框/组合框的旧值,那么保存按钮应该被禁用。
在您的RelayCommand中可能是Func<object, bool> canExecute
的第二个参数。然后,您可以添加方法来检查值是否已更改。
private bool CanSaveCommandExecute(object parameter) => this.Size != defaultValue;
在构造函数中添加SaveCommand中该方法的名称作为第二个参数。我希望这对你有帮助。