代码之家  ›  专栏  ›  技术社区  ›  PiotrK

UserControl在拥有的窗口wpf中导航

  •  0
  • PiotrK  · 技术社区  · 9 年前

    在XAML/WPF中,我有一个主窗口,其中包含我打算为给定的应用程序视图放置一个用户控件的框架。

    <Window x:Class="MyApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ignore="http://www.galasoft.ch/ignore"
        mc:Ignorable="d ignore"
        DataContext="{Binding Main, Source={StaticResource Locator}}">
        <Grid x:Name="LayoutRoot">
            <Frame Source="Main/MainUserControl.xaml" Name="Main" />
        </Grid>
    </Window>
    

    现在,我想将此帧导航到MainUserControl内的其他源:

    <UserControl x:Class="MyApp.View.MainMenu.MainMenuUserControl"
             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:lex="http://wpflocalizeextension.codeplex.com"
             xmlns:command="clr-namespace:MyApp.Command"
             mc:Ignorable="d" 
             Style="{StaticResource Localizable}"
             DataContext="{Binding MainMenu, Source={StaticResource Locator}}">
    
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <Button Content="{lex:Loc About}" FontSize="28" Grid.Row="1" Command="NavigationCommands.GoToPage" CommandParameter="/Menu/AboutUserControl.xaml" />
        </Grid>
    </UserControl>
    

    但导航按钮“关于”在执行期间保持不活动状态。我正确验证了/Menu/AboutUserControl。xaml存在。

    我显然做错了什么。如何从用户控件中导航所属窗口的框架?最好通过XAML?

    1 回复  |  直到 9 年前
        1
  •  1
  •   Kevin K    9 年前

    我假设您使用的是MVVM框架。(我已经在这里添加了关键元素,以防您没有添加)。

    您的主窗口。xaml应该使用“ItemsControl”而不是“Frame”。框架可以工作,但更好的方法是像这样使用ItemsControl:

    <!-- Main Frame -->
    <Grid Grid.Column="1" Margin="10" Name="MainWindowFrameContent">
        <ItemsControl ItemsSource="{Binding Path=MainWindowFrameContent}" >
    
            <!-- This controls the height automatically of the user control -->
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="1" IsItemsHost="True"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Grid>
    

    在主窗口的构造函数中。cs,我将窗口的DataContext设置为MainViewModel:

    using myProject.ViewModel;
    
    public partial class MainWindow : Window
    {
        MainViewModel mMainViewModel;
    
        public MainWindow()
        {
            InitializeComponent();
    
            // Initialize MainViewModel and set data context to the VM
            mMainViewModel = new MainViewModel();
            DataContext = mMainViewModel;
        }
    }
    

    (我不确定下一部分是否必须是一个可观察的集合,但我已经这样实现了,它似乎工作得很好。唯一的缺点是,我需要在添加新的UserControl之前手动清除ItemsControl)

    我的MainViewModel实现了名为“MainWindowFrameContent”的绑定。我的所有用户控件都在MainViewModel中初始化。cs代码。我为每个UserControl都有一个额外的ViewModel,并在将UserControl显示到主窗口之前,将UserControl的DataContext分配给各个ViewModel。

    我的MainViewModel.cs:

    public class MainViewModel : ObservableObject
    {
        public MainViewModel()
        {
        }
    
        // This handles adding framework (UI) elements to the main window frame
        ObservableCollection<FrameworkElement> _MainWindowFrameContent = new ObservableCollection<FrameworkElement>();
        public ObservableCollection<FrameworkElement> MainWindowFrameContent
        {
            get
            {
                return _MainWindowFrameContent;
            }
            set
            {
                _MainWindowFrameContent = value;
                RaisePropertyChangedEvent("MainWindowFrameContent");
            }
        }
    
        // This handles opening a generic user control on the main window
        // The ICommand implementation allows me to bind the command of a button on the main window to displaying a specific page
        public ICommand MainWindowDataEntryView
        {
            get
            {
                return new DelegateCommand(_MainWindowDataEntryView);
            }
        }
        void _MainWindowDataEntryView(object obj)
        {
            DataEntryVM wDataEntryVM = new DataEntryVM();
            DataEntryView wDataEntryView = new DataEntryView();
            wDataEntryView.DataContext = wDataEntryVM;
    
            MainWindowFrameContent.Clear();
            MainWindowFrameContent.Add(wDataEntryView);
        }
    }
    

    然后你需要确保你有一个ObservableObject。cs作为项目的一部分:

    using System.ComponentModel;
    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void RaisePropertyChangedEvent(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    您需要一个DelegateCommand。cs类作为项目的一部分:

    using System;
    using System.Windows.Input;
    public class DelegateCommand : ICommand
    {
        private readonly Action<object> _action;
    
        public DelegateCommand(Action<object> action)
        {
            _action = action;
        }
    
        public void Execute(object parameter)
        {
            _action(parameter);
        }
    
        public bool CanExecute(object parameter)
        {
            return true;
        }
    
    #pragma warning disable 67
        public event EventHandler CanExecuteChanged { add { } remove { } }
    #pragma warning restore 67
    }
    

    所以,这是一个有点冗长的解释,但一旦您设置了前面的项目,您就可以向主窗口添加一系列按钮。xaml,将每个按钮绑定到一个命令,该命令将新的UserControl添加到ItemsControl。显示UserControl时,可以根据需要添加控件并使用它们。

    我希望这有帮助!

    推荐文章