티스토리 뷰

C#/WPF

MVVM 예제 - Multiple Views

광그로 2017. 7. 10. 11:03

https://www.youtube.com/watch?v=xUwk2-_tRzo


View 폴더 - 사용자정의컨트롤로 FirstView, SecondView, ThirdView를 생성


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<Window x:Class="MVVM_MultipleView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MVVM_MultipleView" 
        xmlns:viewmodels="clr-namespace:MVVM_MultipleView.ViewModel"
        xmlns:views="clr-namespace:MVVM_MultipleView.View"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
-------------------------------------------------------------------------------------------------
    <Window.Resources>
        <DataTemplate x:Name="firstViewTemplate" DataType="{x:Type viewmodels:FirstViewModel}">
            <views:FirstView DataContext="{Binding}"/>
        </DataTemplate>
        <DataTemplate x:Name="secondViewTemplate" DataType="{x:Type viewmodels:SecondViewModel}">
            <views:SecondView DataContext="{Binding}"/>
        </DataTemplate>
        <DataTemplate x:Name="thirdViewTemplate" DataType="{x:Type viewmodels:ThirdViewModel}">
            <views:ThirdView DataContext="{Binding}"/>
        </DataTemplate>
    </Window.Resources>
-------------------------------------------------------------------------------------------------

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <DockPanel Background="Gray" Grid.Row="0" Grid.Column="0" Grid.RowSpan="5">
            <StackPanel>
                <Button Content="First" Click="FirstView_Clicked"></Button>
                <Button Content="Second" Click="SecondView_Clicked"></Button>
                <Button Content="Third" Click="ThirdView_Clicked"></Button>
            </StackPanel>
        </DockPanel>
 
        <ContentControl Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="4" Grid.RowSpan="5" 
                        Content="{Binding }"></ContentControl>
    </Grid>
</Window>
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using MVVM_MultipleView.ViewModel;
namespace MVVM_MultipleView
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void FirstView_Clicked(object sender, RoutedEventArgs e)
        {
            DataContext = new FirstViewModel();
 
        }
 
        private void SecondView_Clicked(object sender, RoutedEventArgs e)
        {
            DataContext = new SecondViewModel();
        }
 
        private void ThirdView_Clicked(object sender, RoutedEventArgs e)
        {
            DataContext = new ThirdViewModel();
        }
    }
}
 
cs


'C# > WPF' 카테고리의 다른 글

MVVM 예제2 - WPF JSON ListView + MVVM  (0) 2017.07.10
MVVM 기본 세팅  (0) 2017.07.09
MVVM 예제1  (0) 2017.07.09
WPF tutorial - ListView sorting  (0) 2017.07.04
WPF tutorial - ListView grouping  (0) 2017.07.04
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함