How to reload (reset) the whole page in WPF?

Related searches

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)

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.

How to: Refresh a Page, This example shows how to call the Refresh method to refresh the current content in a NavigationWindow. Example. Refresh refreshes the current content in a NavigationWindow to be reloaded from View all page feedback. Refresh refreshes the current content in a NavigationWindow to be reloaded from its source. void navigateRefreshButton_Click(object sender, RoutedEventArgs e) { this.Refresh(); } Private Sub navigateRefreshButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Me.Refresh() End Sub

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;
        }

[C#/WPF] Forcing a page to reload PageLoad. : learnprogramming, However, when I restart the app and return to Page B, the listbox displays the updated information from the Thanks a lot for all your comments and the awards!! I used a third library to draw 3D modal on window,if i create a Winform, and refresh view in OnPaint Event of window, it is correct and no problem. But if i create a WPF window, i can't find the Event which could be used to refresh the view, so the view disappear immediately after it display.

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;
    }

How to reload (reset) the whole page in WPF? - c# - csharp, I display Request.xaml this way: first of all, MainWindow.xaml displays MainPage. xaml, <mui:Link DisplayName="Generation" Source="/Pages/MainPage.xaml"� One nice way to refresh the data content of the page is to refresh the binding context - page.DataContext = null; page.DataContext = dataModel; To re-render a page, try the solution at Update the WPF UI now: how to wait for the rendering to finish ?.

Avoid full page refresh on button click, Quote: the grid contains 10 rows. I am smelling that this code belongs to ASP. NET Web Forms, if so then you need to make sure that each time� The class should be transparent, so the user should not have to change his existing view model in any way to use this class. This code will only work with WPF, and I have tested this with .NET versions 4.0 (full and client profile) as well as 3.5 (full and client profile).

Resetting a View Model in WPF MVVM applications without code , Why use this class at all? Basarat Ali Syed wondered why I even needed to write this class. His exact words were: Why not just use a ViewModel� You can refresh, reset, or restore your PC from the Windows Recovery Environment. If you want to back up and restore your personal files using File History, see Set up a drive for File History. Before you start to refresh or reset your PC. In most cases, once you start to refresh or reset your PC, it’ll finish on its own.

XAML Hot Reload plugs into your existing workflow to increase your productivity and save you time. Without XAML Hot Reload, you have to build and deploy your app every time you want to see a XAML change. With Hot Reload, when you save your XAML file the changes are reflected live in your running app.

Comments
  • You need to show us some code. What have you tried so far?
  • Come on now @Rocketq... you've been a member here long enough to know that we don't ask questions like that. If you don't know that, then please take a look at the Asking page of the help centre.
  • Are you binding to properties in the code - behind?
  • @Rocketq, on this website, there are rules to asking questions... rules that you have not followed, perhaps not even read. I provided you with a link to a relevant page in my last comment. You need to demonstrate to us that you have some knowledge of the question subject and that you have at least attempted to accomplish your requirements on your own. You have done neither. This is not a site where you can get people to do your work for you.
  • Ok thanks for updating... Finally, how are you displaying the Request.xaml view in the MainWindow.xaml page?
  • Why do you need to set it to null first?
  • @YoupTube VS 2013 says me, that there is no such member page, what shoud I put instead of it? And do I have to put it here Button_Click?
  • It may be window.DataContext as well, just the instance of the container that holds all ui elements and that instance where you set the datacontext. It may be a frame, a grid, a stackpanel whatever...