如何在 WPF 中重新加载(重置)整个页面



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

我的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>    

此外,xaml 编码此类事件<Button Content="Next request" Width="160" VerticalAlignment="Bottom" Background="#FF339933" Click="Button_Click" />

Request.xaml.cs文件只有private void Button_Click(object sender, RoutedEventArgs e)功能。

我以这种方式显示Request.xaml:首先,MainWindow.xaml显示MainPage.xaml<mui:Link DisplayName="Generation" Source="/Pages/MainPage.xaml" /> , 最后MainPage.xaml支付了Request.xaml">

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

可以通过OnNavigatedTo方法UIElement.InvalidateVisual Method(http://msdn.microsoft.com/en-us/library/system.windows.uielement.invalidatevisual.aspx(来实现。

当然有可能!但。。。是否将组合框数据绑定到某个基础对象实例?

然后,您可以轻松地以"困难"的方式进行操作并设置

page.DataContext = null;
page.DataContext = new Foo();

然后,所有数据绑定都将使用"默认"值重新初始化。

到目前为止,我不使用 MVVM/DataContext,所以在这种特殊情况下,只有一种方法可以将值设置为默认值,那就是手动完成。

TxtBlock_numRequest.Text = "Default";

但是这个解决方案看起来真的很糟糕,但至少它有效。

解决此问题的另一种方法是使用 MVVM 和数据绑定。此解决方案由@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;
        }

如果您想保持在同一页面上但清除所有字段,例如,如果页面的 DataContext 需要使用参数创建,您可以简单地将这样的方法添加到页面或用户控件的 ...XAML.cs文件:

    private void Clear(object sender, RoutedEventArgs e)
    {
        this.DataContext = null;
    }

最新更新