您的位置:首页 > 编程语言 > C#

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

2016-03-02 11:25 411 查看
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;

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  软件 实例 函数 wpf c#