如何在 WPF 中重新加载(重置)整个页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20068788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reload (reset) the whole page in WPF?
提问by Rocketq
I have Request.xamlwith button and with many comboxes, so I want reload it and put combox values to put it to default after button click. Of course I do some more staff.
我有Request.xaml按钮和许多组合框,所以我想重新加载它并将组合框值放在按钮单击后将其设置为默认值。当然,我会做更多的员工。
My Request.xamlcode has such parts of the code:
我的Request.xaml代码有这样的代码部分:
<TextBox x:Name="TxtBlock_numRequest" TextWrapping="Wrap" Height="23"/>
<ComboBox x:Name="CmbBox_lvlPriority" Width="160">
<ComboBoxItem Content="1" Name="High" />
<ComboBoxItem Content="2" Name="Medium" />
<ComboBoxItem Content="3" Name="Low" />
</ComboBox>
In addition, xaml code such event<Button Content="Next request" Width="160" VerticalAlignment="Bottom" Background="#FF339933" Click="Button_Click" />
此外,xaml 编码此类事件<Button Content="Next request" Width="160" VerticalAlignment="Bottom" Background="#FF339933" Click="Button_Click" />
And Request.xaml.csfile have just private void Button_Click(object sender, RoutedEventArgs e)function.
和Request.xaml.cs文件只是private void Button_Click(object sender, RoutedEventArgs e)功能。
I display Request.xamlthis way: first of all, MainWindow.xamldisplays MainPage.xaml,
<mui:Link DisplayName="Generation" Source="/Pages/MainPage.xaml" />,
and finally MainPage.xamldispays Request.xaml`
我显示Request.xaml是这样的:首先, MainWindow.xaml显示器MainPage.xaml,
<mui:Link DisplayName="Generation" Source="/Pages/MainPage.xaml" />以及最后MainPage.xamldispays Request.xaml`
Is it possible to reset the whole page, because I need to give user opportunity to add new request's parameters to existing parameters, which eventually will be located in a .xmlfile?
是否可以重置整个页面,因为我需要让用户有机会将新请求的参数添加到现有参数中,这些参数最终将位于 .xml文件中?
May be it is possible to realize via OnNavigatedTo Methodor by UIElement.InvalidateVisual Method(http://msdn.microsoft.com/en-us/library/system.windows.uielement.invalidatevisual.aspx)
可能可以通过OnNavigatedTo 方法或UIElement.InvalidateVisual 方法(http://msdn.microsoft.com/en-us/library/system.windows.uielement.invalidatevisual.aspx)实现
回答by Bernoulli IT
Off course it's possible! But...do you databind the comboboxes to some underlying object instance?
当然有可能!但是...您是否将组合框数据绑定到某个底层对象实例?
Then you can easily do it the "hard" way and set
然后您可以轻松地以“困难”的方式进行并设置
page.DataContext = null;
page.DataContext = new Foo();
Then all databinding will be re-initialized with the "default" values.
然后所有数据绑定都将使用“默认”值重新初始化。
回答by Rocketq
As far I don`t use MVVM/DataContext, so in that particulal case there is only one way out to set values to default, it is to do it by hand.
就我不使用 MVVM/DataContext 而言,因此在这种特殊情况下,只有一种方法可以将值设置为默认值,那就是手动完成。
TxtBlock_numRequest.Text = "Default";
But this solution looks really bad, but at least it works.
但是这个解决方案看起来很糟糕,但至少它是有效的。
Another way to solve this problem is to use MVVM and DataBinding. This solution was given by @ZeroART:
解决这个问题的另一种方法是使用 MVVM 和 DataBinding。这个解决方案是由@ZeroART 给出的:
//XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBox x:Name="TextBox1" Width="200" HorizontalAlignment="Left" Text="{Binding TextValue, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<ComboBox x:Name="ComboBox1" HorizontalAlignment="Left" ItemsSource="{Binding Items}" SelectedValue="{Binding SelectedValue, UpdateSourceTrigger=PropertyChanged}" Width="200"/>
<Button x:Name="Button1" HorizontalAlignment="Left" Content="Save" Command="{Binding ClickCommand}" Width="116"/>
</StackPanel>
</Window>
//
//
//ViewModel
public class MainViewModel : INotifyPropertyChanged
{
private IList<string> _items;
private bool _canExecute;
private ICommand _clickCommand;
private string _textValue;
private string _selectedValue;
public IList<string> Items
{
get { return _items; }
}
public string SelectedValue
{
get { return _selectedValue; }
set
{
_selectedValue = value;
OnPropertyChanged("SelectedValue");
}
}
public string TextValue
{
get { return _textValue; }
set {
_textValue = value;
OnPropertyChanged("TextValue");}
}
public void Save()
{
SelectedValue = _items.FirstOrDefault();
TextValue = "Значение по умолчанию";
}
public ICommand ClickCommand
{
get { return _clickCommand ?? (new RelayCommand(() => Save(), _canExecute)); }
}
public MainViewModel()
{
_items = new List<string> { "Test1", "Test2", "Test3" };
_selectedValue = _items.First();
_textValue = "Значение по умолчанию";
_canExecute = true;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class RelayCommand : ICommand
{
private Action _action;
private bool _canExecute;
public RelayCommand(Action action, bool canExecute)
{
_action = action;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action();
}
}
Plus we need this:
另外我们需要这个:
private readonly MainViewModel _viewModel;
public MainWindow()
{
InitializeComponent();
_viewModel = new MainViewModel();
this.DataContext = _viewModel;
}
回答by Nicolette Anderson
In the event you want to stay on the same page but clear all fields, such as if the page's DataContext needs to created with parameters, you can simply add a method like this to the page or user control's ...xaml.cs file:
如果您想留在同一页面但清除所有字段,例如如果页面的 DataContext 需要使用参数创建,您可以简单地将这样的方法添加到页面或用户控件的 ...xaml.cs 文件中:
private void Clear(object sender, RoutedEventArgs e)
{
this.DataContext = null;
}

