c# - 如何动态更改 wpf MVVM light 中用户控件中存在的按钮(单击)上的用户控件

标签 c# wpf mvvm user-controls

我有一个主窗口,它将 Usercontrol 托管为 ContentControl 主机。我想要的是,将按钮单击时的 usercontrol(出现在第一个 Usercontrol 中)动态更改为另一个 usercontrol。

目前我在主窗口资源中创建了一个 DataTemplate,其中包含用户控件的相应 ViewModel

 <p></p>

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

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

<p></p>

我想在 view1 中单击按钮时从 View1 更改为 view2。那么ViewModel1(US1 viewModel) 怎么改成US2

我目前正在研究 MVVM light。

我有一个服务定位器,其中包含每个 VM 的注册实例。问题是我如何指向 VM1 中的 VM2 实例。

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

最佳答案

将您的窗口视为 shell 并使用 MvvmLight 的 Messenger 将消息发送到您的 shell 以交换 View 。

例如:

主窗口.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

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

<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

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" });
                });
            }
        }
    }
}

第二 View .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

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

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

关于c# - 如何动态更改 wpf MVVM light 中用户控件中存在的按钮(单击)上的用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21107957/

相关文章:

c# - Symantec ScanEngine API 关于 StreamScanRequest 的说明

c# - 如何单击嵌套在数据网格内的数据网格中的复选框

c# - 如何将文本框的文本双向数据绑定(bind)到依赖项属性

wpf - MVVM 模型责任

wpf - 查看控件显示/隐藏 WPF MVVM

c# - 为什么 default 不适用于双括号?

c# - 用于从控制台应用程序打印调试信息的 Console.Writeline() 替代方法?

c# - ASP.Net 标识 : Difference between UseOAuthBearerTokens and UseCookieAuthentication?

WPF - 在页面/窗口级别响应 ViewModel 更改的最佳方式

c# - 使用 ObservableCollection 和绑定(bind) WPF