重置WPF应用程序中的所有绑定



我有一个WPF应用程序,它有很多文本框、组合框等。有两种模式:

  1. 新鲜
  2. 加载现有配置

如果您选择Fresh模式,您将在";MyConfigs.json";像这样加载的文件:

public static string currentDirectory = Directory.GetCurrentDirectory();
public static string json = File.ReadAllText(currentDirectory + "/Important/MyConfigs.json");
public static AppMyConfigs Default = JsonConvert.DeserializeObject<AppMyConfigs>(json);

当我开始新的安装时,这个MyConfigs.json已经用完了,它包含了一堆默认值,这些值最初用于用值填充文本框等。其中不存在的值为空,所有这些都可以。

但是,我有另一种模式,可以使用Load existing configurations。在这种模式下,我可以加载一个包含所有配置的.json文件,然后用加载的特定.json文件中的值替换文本框等中的值。

我现在的问题是,如果我加载现有配置,然后返回并选择Fresh模式,则会显示以前加载的值。我希望当我选择Fresh模式时,它应该加载以前的值;重新初始化绑定";。

示例文本框的XAML如下所示:

<StackPanel x:Name="StackName" Orientation="Vertical" Margin="25,25,25,5">
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
<TextBox materialDesign:HintAssist.Hint="Please enter your name*" materialDesign:HintAssist.HintOpacity="10" materialDesign:HintAssist.Background="White" materialDesign:ValidationAssist.Background="White" materialDesign:HintAssist.Foreground="#FF002655" Style="{StaticResource MaterialDesignOutlinedTextFieldTextBox}" x:Name="txtName" Width="230" TextChanged="txtName_TextChanged">
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" Source="{StaticResource MyConfigs}" >
<Binding.ValidationRules>
<local:TextBoxNotEmptyValidation ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
</TextBox>
</StackPanel>
</StackPanel>

后面的代码看起来像:

private void txtName_TextChanged(object sender, TextChangedEventArgs e)
{
MyConfigs.GlobalName = txtName.Text;
}

最初在Fresh模式中,MyConfigs.Name的值被设置为John。然而,一旦我加载了一个名为Mark的不同.json文件,即使在我进入Fresh模式之后,该名称也将保持为Mark

您必须使用数据绑定。

您需要数据源:每个模式/数据视图一个
TextBox.Text属性绑定到数据源上的属性
根据当前模式,您将关联的数据模型设置为数据绑定的DataContext

DataModel.cs
输入表单的数据源.

class DataModel : INotifyPropertyChanged
{ 
private string dataField;
public string DataField
{
get => this.dataField;
set
{
this.dataField = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
=> this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

主窗口.xaml.cs

partial class MainWindow : Window
{
private DataModel FreshData { get; set; }
private DataModel ExistingData { get; set; }
public MainWindow()
{
InitializeComponent();

// Initialize a new DataModel instance from the default configuration file
this.FreshData = CreateDataModelFromDefaultConfiguration();
this.DataContext = this.FreshData;
}
private void OnLoadDefaultDataMode_Click(object sender, EventArgs e)
{
// Initialize a new DataModel instance from the default configuration file
this.FreshData = CreateDataModelFromDefaultConfiguration();
// Update all data bindings
this.DataContext = this.FreshData;
}
private void OnLoadExistingData_Click(object sender, EventArgs e)
{
// Initialize a new DataModel instance from an existing configuration file
this.ExistingData = CreateDataModelFromExistingConfiguration();

// Update all data bindings
this.DataContext = this.ExistingData;
}    
}

主窗口.xaml

<Window>
<TextBox Text={Binding DataField}" />
</Window>

请参阅Microsoft文档:数据绑定概述

如果两个数据模型都需要实现各自的逻辑,那么为每个模式/数据视图创建一个专用的数据模型类(而不是像本例中那样使用两个相同类型的实例(。

最新更新