WPF 加UserControl(wpf) ViewModel模式数据之间交互

1、<Frame x:Name="frameChild"  Source="1UserControl.xaml" Navigated="frameChild_Navigated"  NavigationUIVisibility="Hidden" Margin="5"/>


2、public 类名VM

        {
            get
            {
                return this.DataContext as 类名;
            }
        }

       private void frameChild_Navigated(object sender, NavigationEventArgs e)
        {
            if ((string)cmbx_mac.SelectedItem == "某个项")
            {
                this.VM.1Vm = (this.frameChild.Content as UserControl).DataContext as 第一个ViewModel;
            }
            else if((string)cmbx_mac.SelectedItem=="另一项")
            {
                this.VM.2Vm = (this.frameChild.Content as UserControl).DataContext as 第二个ViewModel;
            }
        }

3、

this.frameChild.Loaded += frameChild_Loaded;

   void frameChild_Loaded(object sender, RoutedEventArgs e)
        {
            if ((string)cmbx_mac.SelectedItem == "某项")
            {
                this.VM.1Vm = (this.frameChild.Content as UserControl).DataContext as 第一个ViewModel;
            }
            else if ((string)cmbx_mac.SelectedItem == "另一项")
            {
                this.VM.2Vm = (this.frameChild.Content as UserControl).DataContext as 第二个ViewModel;
            }
        }

4、 <ComboBox x:Name="cmbx_mac" ItemsSource="{Binding A}" SelectedItem="{Binding B}" 
                     SelectionChanged="SelectionChanged_page"/>


 private void SelectionChanged_page(object sender, SelectionChangedEventArgs e)
        {
            if ((string)cmbx_mac.SelectedItem == "某项")
            {
                this.frameChild.Source = new Uri("1.xaml", UriKind.Relative);
            }
            else if ((string)cmbx_mac.SelectedItem == "另一项")
            {
                this.frameChild.Source = new Uri("2.xaml", UriKind.Relative);
            }
        }

5、MainViewModel里


 public bool hasChanged { get; set; }
        #region
        public event PropertyChangedEventHandler PropertyChanged;


        public void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));


                this.hasChanged = true;
            }
        }
        #endregion


        public 第一个ViewModel 1Vm
        {
            get;
            set;
        }
        public 另一个ViewModel 2Vm
        {
            get;
            set;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值