WPF 设置Frame中Page的DataContext

摘要:
WPF窗体MainWindow中有Frame控件,名为MainFrame,MainFrame通过ViewModel绑定Source属性来设置显示的Page页,其中的Page页需要与MainWindow共用一个ViewModel对象做DataContextMainWindow.xaml˂BorderMargin="5"Background="White"CornerRadius="4"Padding

WPF窗体MainWindow中有 Frame控件,名为 MainFrame,
MainFrame 通过ViewModel绑定Source属性来设置显示的Page页,
其中的Page页需要与MainWindow 共用一个ViewModel对象做DataContext

MainWindow.xaml

<Border Margin="5" Background="White" CornerRadius="4" Padding="6" Grid.Row="1">
                <Frame Margin="0" x:Name="MainFrame" NavigationUIVisibility="Hidden" LoadCompleted="MainFrame_LoadCompleted" Source="{Binding PageUri,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
</Border>

MainWindow.xaml.cs


        public MainWindow()
        {
            InitializeComponent();
            _viewModel = new ViewModel();
            this.DataContext = _viewModel;
        }
        
        private void MainFrame_LoadCompleted(object sender, NavigationEventArgs e)
        {
            var content = MainFrame.Content as FrameworkElement;//获取当前Frame中的Page
            if (content == null)
            {
                return;
            }
            content.DataContext = this.DataContext;//设置Page的DataContext
        }

ViewModel.cs

private Uri pageUri;
//当前Frame显示的PageUri,即Source属性值,通过设置此属性值来管理Frame的显示
        public Uri PageUri {
            get
            {
                if (pageUri==null)
                {
                    pageUri = new Uri("Pages/Page1.xaml",UriKind.Relative);
                }
                return pageUri;
            }

            set
            {
                pageUri = value;
                OnPropertyChanged(nameof(PageUri));
            }
        }

ViewModelBase.cs

public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

免责声明:文章转载自《WPF 设置Frame中Page的DataContext》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇php session 数据保存在哪里?HTTP.SYS 远程执行代码漏洞分析(MS15-034 )下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF模式思考 (zt)

Introduction Since XAML things have become a bit complicated in trying to conceptualize MVC architectures for Windows applications. The gap between web and win is narrowing and th...

WPF双向绑定

数据绑定模式共有四种:OneTime、OneWay、OneWayToSource和TwoWay,默认是TwoWay。 TwoWay 当发生更改时的目标属性或源属性更新目标属性。OneWay 仅当源属性更改时,请更新目标属性。OneTime 仅当应用程序启动时或时,请更新目标属性DataContext发生了更改。OneWayToSource 目标属性更改时,...

走进WPF之UI布局

一个成功的软件,离不开人性化的UI设计,如何抓住用户第一视觉,让用户产生依赖感,合适优雅的布局必不可少。本文以一些简单的小例子,简述WPF中布局面板控件的使用,仅供学习分享使用,如有不足之处,还请指正。 涉及知识点 在WPF中,关于布局面板控件,主要有以下几种: StackPanel:栈面板,可以将元素排列成一行或者一列。其特点是:每个元素各占一行或者一...

C#后台实现在Grid标签中动态新增CheckBox标签(WPF中)

页面代码 1 <Grid Margin="45,0,10,0" > 2 <Grid.RowDefinitions> 3 <RowDefinition Height="*"/> 4 <RowDef...

wpf触发器

1.属性触发器(依赖属性皆可,有个疑问按钮点击一下一直在闪,待研究) <Style.Triggers><TriggerProperty="IsMouseOver"Value="True"><SetterProperty="Background"Value="Green"/></Trigger></Sty...

wpf中的全局键

在wpf中的全局键和form中有些差异,所以在这里记录一下,有用到得朋友可以试试! 首先注册APi 代码 1  [DllImport("user32.dll")]2  public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control, System.Windows.For...