如何在 WPF 中重新加载(重置)整个页面? [英] How to reload (reset) the whole page in WPF?

查看:42
本文介绍了如何在 WPF 中重新加载(重置)整个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有按钮和许多组合框的 Request.xaml,所以我想重新加载它并将组合框值放在按钮单击后将其设置为默认值.当然,我还有更多的员工.

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.

我的 Request.xaml 代码有这样的代码部分:

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>    

此外,xaml 代码如 event <Button Content="Next request" Width="160" VerticalAlignment="Bottom" Background="#FF339933" Click="Button_Click"/>

In addition, xaml code such event <Button Content="Next request" Width="160" VerticalAlignment="Bottom" Background="#FF339933" Click="Button_Click" />

Request.xaml.cs 文件只有 private void Button_Click(object sender, RoutedEventArgs e) 函数.

And Request.xaml.cs file have just private void Button_Click(object sender, RoutedEventArgs e) function.

我这样显示Request.xaml:首先,MainWindow.xaml显示MainPage.xaml<mui:Link DisplayName="Generation" Source="/Pages/MainPage.xaml"/>,最后 MainPage.xaml 显示 Request.xaml`

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`

是否可以重置整个页面,因为我需要让用户有机会将新请求的参数添加到现有参数中,这些参数最终将位于 .xml 文件中?

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?

也许可以通过OnNavigatedTo 方法UIElement.InvalidateVisual 方法(http://msdn.microsoft.com/en-us/library/system.windows.uielement.invalidatevisual.aspx)

推荐答案

至于我不使用 MVVM/DataContext,所以在这种特殊情况下,只有一种方法可以将值设置为默认值,就是这样做手动.

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.

另一种解决这个问题的方法是使用MVVM和DataBinding.此解决方案由@ZeroART 提供:

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

另外,我们需要这个:

private readonly MainViewModel _viewModel;
public MainWindow()
        {
            InitializeComponent();
            _viewModel = new MainViewModel();
            this.DataContext = _viewModel;
        }

这篇关于如何在 WPF 中重新加载(重置)整个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆