2

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)

Rocketq
  • 5,423
  • 23
  • 75
  • 126
  • You need to show us some code. What have you tried so far? – David Pilkington Nov 19 '13 at 10:18
  • 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](http://stackoverflow.com/help/asking) of the help centre. – Sheridan Nov 19 '13 at 10:29
  • Are you binding to properties in the code - behind? – David Pilkington Nov 19 '13 at 11:47
  • @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. – Sheridan Nov 19 '13 at 11:52
  • Good. However, you still need to explain the connection/relationship between your `Click` handler, the XAML that you have shown and `Page.xaml`. Also, do you want to reset the selected item of your `ComboBox`es, or reset the whole page? – Sheridan Nov 19 '13 at 12:03
  • @DavidPilkington User can type some text into `Textbox`, but I dont change it at all/ – Rocketq Nov 19 '13 at 12:04
  • 1
    Ok thanks for updating... Finally, how are you displaying the `Request.xaml` view in the `MainWindow.xaml` page? – Sheridan Nov 19 '13 at 12:10
  • @Sheridan I though, my problem might be can be easily solved – Rocketq Nov 19 '13 at 21:03
  • Maybe it could... with the right information. I'm a bit lost with your way of working because usually in WPF we manipulate data objects rather than UI objects. In that case, we could refresh a view simply by creating a new object that is bound to the UI. – Sheridan Nov 19 '13 at 22:54

3 Answers3

2

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.

Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
  • 1
    Why do you need to set it to `null` first? – David Pilkington Nov 19 '13 at 11:48
  • @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`? – Rocketq Nov 19 '13 at 12:33
  • 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... – Youp Bernoulli Nov 20 '13 at 09:42
1

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;
        }
Rocketq
  • 5,423
  • 23
  • 75
  • 126
0

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