如何动态地更改用户控件按钮(点击)出现在用户控件在WPF MVVM光 [英] how to dynamically change userControl on button (click) present in the usercontrol in wpf MVVM light

查看:302
本文介绍了如何动态地更改用户控件按钮(点击)出现在用户控件在WPF MVVM光的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主窗口主机用户控件作为ContentControl中host.What我要的是,动态地改变用户控件上按一下按钮(存在于第一个用户控件)到另一个用户控件。

I have a Main Window that host Usercontrol as ContentControl host.What i want is, to dynamically change usercontrol on button click(present in the first Usercontrol ) to another usercontrol.

目前我已经在由用户控件的各个视图模型的主窗口资源创建一个DataTemplate

Currently I have created a DataTemplate in the Main Window resources consisting of the respective ViewModel of the usercontrol

 
<DataTemplate DataType="{x:Type Tube:ViewModel1}" >
        <Tube:View1/>
 </DataTemplate>

 <DataTemplate DataType="{x:Type Tube1:ViewModel2}">
        <Tube2:View2/>
 </DataTemplate>



我想从视图1切换到视图2上单击按钮出现在厂景。所以,我应该做的ViewModel1(US1视图模型)更改为1686

I want to change to from View1 to view2 on button click present in view1. So what should i do in ViewModel1(US1 viewModel) to change to US2

我目前工作的MVVM光。

I am currently working on MVVM light.

我有有注册的实例每个VM的服务定位器。问题是我怎么能指向VM1 VM2实例。

I have a service locator that has the registered instance of each VM. The problem is how can i point to VM2 instance in VM1.

任何帮助,欢迎!!!!!

Any help is welcomed!!!!!

推荐答案

对待你的窗口,作为壳以便交换意见发送使用MvvmLight的Messenger消息到你的shell

Treat your Window as shell and send messages using MvvmLight's Messenger to your shell in order to swap views.

例如:

MainWindow.xaml

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainWindowViewModel></local:MainWindowViewModel>
    </Window.DataContext>
    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Grid.Column="0" Command="{Binding ChangeFirstViewCommand}">Change View #1</Button>
        <Button Grid.Row="0" Grid.Column="1" Command="{Binding ChangeSecondViewCommand}">Change View #2</Button>
        <ContentControl  Grid.Row="1" Grid.ColumnSpan="2" Content="{Binding ContentControlView}"></ContentControl>
    </Grid>
</Window>



MainWindowViewModel.cs

MainWindowViewModel.cs

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public class MainWindowViewModel : ViewModelBase
    {
        private FrameworkElement _contentControlView;
        public FrameworkElement ContentControlView
        {
            get { return _contentControlView; }
            set
            {
                _contentControlView = value;
                RaisePropertyChanged("ContentControlView");
            }
        }

        public MainWindowViewModel()
        {
            Messenger.Default.Register<SwitchViewMessage>(this, (switchViewMessage) =>
            {
                SwitchView(switchViewMessage.ViewName);
            });
        }

        public ICommand ChangeFirstViewCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    SwitchView("FirstView");

                });
            }
        }


        public ICommand ChangeSecondViewCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    SwitchView("SecondView");
                });
            }
        }

        public void SwitchView(string viewName)
        {
            switch (viewName)
            {
                case "FirstView":
                    ContentControlView = new FirstView();
                    ContentControlView.DataContext = new FirstViewModel() { Text = "This is the first View" };
                    break;

                default:
                    ContentControlView = new SecondView();
                    ContentControlView.DataContext = new SecondViewModel() { Text = "This is the second View" };
                    break;
            }
        }
    }
}



的firstView的.xaml

FirstView.xaml

<UserControl x:Class="WpfApplication1.FirstView"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel>
        <Label>This is the second view</Label>
        <Label Content="{Binding Text}" />
        <Button Command="{Binding ChangeToSecondViewCommand}">Change to Second View</Button>
    </StackPanel>
</UserControl>



FirstViewModel.cs

FirstViewModel.cs

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApplication1
{
    public class FirstViewModel : ViewModelBase
    {

        private string _text;
        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                RaisePropertyChanged("Text");
            }
        }

        public ICommand ChangeToSecondViewCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    Messenger.Default.Send<SwitchViewMessage>(new SwitchViewMessage { ViewName = "SecondView" });
                });
            }
        }
    }
}



SecondView的.xaml

SecondView.xaml

<UserControl x:Class="WpfApplication1.SecondView"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel>
        <Label>This is the second view</Label>
        <Label Content="{Binding Text}" />
        <Button Command="{Binding ChangeToFirstViewCommand}">Change to First View</Button>
    </StackPanel>
</UserControl>



SecondViewModel.cs

SecondViewModel.cs

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApplication1
{
    public class SecondViewModel : ViewModelBase
    {

        private string _text;
        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                RaisePropertyChanged("Text");
            }
        }

        public ICommand ChangeToFirstViewCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    Messenger.Default.Send<SwitchViewMessage>(new SwitchViewMessage { ViewName = "FirstView" });
                });
            }
        }
    }
}



SwitchViewMessage的.cs

SwitchViewMessage.cs

namespace WpfApplication1
{
    public class SwitchViewMessage
    {
        public string ViewName { get; set; }
    }
}

这篇关于如何动态地更改用户控件按钮(点击)出现在用户控件在WPF MVVM光的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆