如何使用WPF和C#创建具有两个子页面的保存/重置选项页面

问题描述 投票:0回答:1

我想创建一个包含两个子页面选项的页面。我遇到的问题是我无法从主页的子页面访问对象。我应该注意,我只使用DataContext来编写脚本。这里有一些代码可以帮助您更好地理解我的意思:

StartPage.xaml

<Page x:Class="WpfApp.StartPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:WpfApp"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300"
  Title="startPage">

<Grid Background="White">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
    <StackPanel>
        <Button Content="Page 1" Command="{Binding FirstPageCommand}"/>
        <Button Content="Page 2" Command="{Binding SecondPageCommand}"/>
    </StackPanel>
    <Frame x:Name="frame" Grid.Column="1" NavigationUIVisibility="Hidden"
           Source="Page1.xaml"/>
    <StackPanel Grid.Column="1" Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button Content="Reset" Margin="10" Command="{Binding ResetCommand}"/>
        <Button Content="Save" Margin="10" Command="{Binding SaveCommand}"/>
    </StackPanel>
</Grid>

StartPage.xaml.cs

using System.Windows.Controls;

namespace WpfApp
{
    /// <summary>
    /// Interaction logic for StartPage.xaml
    /// </summary>
    public partial class StartPage : Page
    {
        public StartPage()
        {
            InitializeComponent();
            DataContext = new StartPage_DataContext(this);
        }
    }
}

StartPage_DataContext.cs

using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApp
{
    public class StartPage_DataContext
    {
        private Page _Page;
        public StartPage_DataContext(Page page)
        {
            _Page = page;

            FirstPageCommand = new RelayCommand(() => FirstPage());
            SecondPageCommand = new RelayCommand(() => SecondPage());
            SaveCommand = new RelayCommand(() => Save());
            ResetCommand = new RelayCommand(() => Reset());
        }

        private void FirstPage()
        {
            (_Page as StartPage).frame.NavigationService.Navigate(new Page1());
        }

        private void SecondPage()
        {
            (_Page as StartPage).frame.NavigationService.Navigate(new Page2());
        }

        private void Save()
        {
            //Here is where I need code for saving both "Page1" and "Page2" elements to Settings class.
            //Exeple : Settings._firstCB = Page1.firstCB.IsCheked.Value;
            //         Settings._secondCB = Page2.firstCB.IsCheked.Value;
        }

        private void Reset()
        {
            //Here is where I need code for setting both "Page1" and "Page2" elements to some default values.
            //Exemple : Page1.firstCB.IsCheked.Value = false;
            //          Page2.firstCB.IsCheked.Value = true;
        }

        public ICommand FirstPageCommand { get; private set; }
        public ICommand SecondPageCommand { get; private set; }
        public ICommand SaveCommand { get; private set; }
        public ICommand ResetCommand { get; private set; }
    }
}

Page1.xaml(第2页类似唯一的区别是元素命名约定)

<Page x:Class="WpfApp.Page1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:WpfApp"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300"
  Title="Page1">

<Grid Background="White">
    <StackPanel>
        <CheckBox x:Name="firstCB" Content="First Combo Box"/>
        <CheckBox x:Name="secondCB" Content="Second Combo Box"/>
        <ComboBox x:Name="firstCombo">
            <ComboBoxItem Content="First Item"/>
            <ComboBoxItem Content="Second Item"/>
        </ComboBox>
    </StackPanel>
</Grid>

c# wpf mvvm datacontext
1个回答
1
投票

不确定您是否有子页面的视图模型,如果有,访问复选框的那些viewmodel属性的一种方法如下所示。

var tt = (((_Page as StartPage).frame.NavigationService.Content as Page1).DataContext as Page1ViewModel).IsCBChecked; 
© www.soinside.com 2019 - 2024. All rights reserved.