问题
I have Request.xaml
with 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.
My Request.xaml
code has such parts of the code:
<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" />
And Request.xaml.cs
file have just private void Button_Click(object sender, RoutedEventArgs e)
function.
I display Request.xaml
this way: first of all, MainWindow.xaml
displays MainPage.xaml
,
<mui:Link DisplayName="Generation" Source="/Pages/MainPage.xaml" />
,
and finally MainPage.xaml
dispays 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 .xml
file?
May be it is possible to realize via OnNavigatedTo Method or by UIElement.InvalidateVisual Method (http://msdn.microsoft.com/en-us/library/system.windows.uielement.invalidatevisual.aspx)
回答1:
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.
回答2:
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.
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:
//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;
}
回答3:
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:
private void Clear(object sender, RoutedEventArgs e)
{
this.DataContext = null;
}
来源:https://stackoverflow.com/questions/20068788/how-to-reload-reset-the-whole-page-in-wpf