WPF:基于按钮点击的动态视图

时间:2015-08-15 06:28:13

标签: wpf dynamic-ui

我是WPF的新手,虽然我阅读了很多理论和文章,但我无法将其全部放在一个有效的解决方案中。

目前,我希望在窗口中实现动态多视图,用户可以使用按钮选择这些视图。目标非常像问题中给出的目标, WPF : dynamic view/content

有人可以与我分享一个工作代码,最简单的实现。只是一个包含两个网格的窗口 - 一个网格有两个按钮 - 第二个网格根据点击的按钮改变背景颜色。从那以后,我可以采取进一步的措施。

非常感谢。

1 个答案:

答案 0 :(得分:4)

使用MVVM

这是一种设计方法。基本上你将你的Window视为shell,他负责交换视图。

为了简化此代码段,我引用了MvvmLight

Window包含动态显示相关视图的ContentControl

每个动态视图都可以与shell Window进行通信(使用MvvmLight' s Messenger)并命令他将视图更改为其他内容

<强> 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

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

<强> 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

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; }
    }
}
相关问题