MVVM Sample for WPF UserControl

MVVM Sample for WPF UserControl

์ด์ฒ ์šฐ

์ด ๊ธ€์—์„œ ๋‘ ๊ฐœ์˜ WPF ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค ๊ฒƒ์ด๋‹ค.
ํ•˜๋‚˜๋Š” WPF UserControl(Net 7) ํ”„๋กœ์ ํŠธ [MvvmSample.WpfUserControl]์ด๊ณ ,
ํ•˜๋‚˜๋Š” WPF(Net 7) ํ”„๋กœ์ ํŠธ [Wpf.App]์ด๋‹ค.

Wpf.App์—์„œ MvvmSample.WpfUserControl๋ฅผ ์ฐธ์กฐํ•  ๊ฒƒ์ด๋‹ค.

1 ํ”„๋กœ์ ํŠธ MvvmSample.WpfUserControl

[CommunityToolkit.Mvvm]๋ฅผ ์ฐธ์กฐ์— ์ถ”๊ฐ€ํ•œ๋‹ค.

dotnet add package CommunityToolkit.Mvvm --version 8.2.2

1-1 Model.cs

๋จผ์ € ๊ธ€ [MVVM Sample for WPF]์˜ [๋ชจ๋ธ]๊ณผ ๊ฐ™๋‹ค.

using System;

namespace MvvmSample.WpfUserControl
{
    public class Model
    {
        public Model() { Data = 0; }
        public int Data
        {
            get => _data;
            set
            {
                if (_data != value)
                {
                    _data = value;
                    Console.WriteLine(this);
                }
            }
        }
        private int _data = -1;

        public override string ToString() => $"Data:{Data}";
    }
}

1-2 ViewModel.cs

๋จผ์ € ๊ธ€ [MVVM Sample for WPF]์˜ [๋ทฐ๋ชจ๋ธ]๊ณผ ๊ฐ™๋‹ค.

using System.Windows.Input;

using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.ComponentModel;

namespace MvvmSample.WpfUserControl
{
    public class ViewModel : ObservableObject
    {
        public readonly Model _model;
        public ViewModel()
        {
            _model = new Model();

            ResetCommand = new RelayCommand(Reset);
            IncreaseCommand = new RelayCommand(Increase);
        }

        public int DataForView
        {
            get => _model.Data;
            set => SetProperty(_model.Data, value, _model, (u, n) => u.Data = n);
        }

        public void Reset()
        {
            DataForView = 0;
        }

        public void Increase()
        {
            DataForView++;
        }

        public ICommand ResetCommand { get; }
        public ICommand IncreaseCommand { get; }
    }
}

1-3 UserControlCounter.xaml

<UserControl x:Class="MvvmSample.WpfUserControl.UserControlCounter"
             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" 
             xmlns:local="clr-namespace:MvvmSample.WpfUserControl"
             mc:Ignorable="d"
             d:DataContext="{d:DesignInstance Type=local:ViewModel, IsDesignTimeCreatable=False}"
             d:DesignHeight="225" d:DesignWidth="400" FontSize="14">
    <Grid>
        <Viewbox Stretch="Uniform">
            <TextBlock Text="{Binding Path=DataForView}" TextAlignment="Center"/>
        </Viewbox>
        <DockPanel VerticalAlignment="Top" HorizontalAlignment="Left">
            <Button Content="Reset" Command="{Binding Path=ResetCommand}"/>
            <Button Content="Increase" Command="{Binding Path=IncreaseCommand}"/>
        </DockPanel>
    </Grid>
</UserControl>

1-4 UserControlCounter.xaml.cs

using System.Windows.Controls;

namespace MvvmSample.WpfUserControl
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControlCounter : UserControl
    {
        public UserControlCounter()
        {
            InitializeComponent();

            DataContext = new ViewModel();
        }
    }
}

2 ํ”„๋กœ์ ํŠธ Wpf.App

ํ”„๋กœ์ ํŠธ [MvvmSample.WpfUserControl]๋ฅผ ์ฐธ์กฐ์— ์ถ”๊ฐ€ํ•œ๋‹ค.

2-1 MainWindow.xaml

<Window x:Class="Wpf.App.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:Wpf.App"
        xmlns:uc="clr-namespace:MvvmSample.WpfUserControl;assembly=MvvmSample.WpfUserControl"
        mc:Ignorable="d"
        Title="MainWindow" Height="225" Width="400" FontSize="14">
    <Grid>
        <uc:UserControlCounter/>
    </Grid>
</Window>

3 ๋‘ ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ์‹คํ–‰

4๊ฐœ์˜ ์ข‹์•„์š”

@leechw9 ์ฒ ์šฐ๋‹˜ ๊ธ€ ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ๋„ MVVM ๊ด€๋ จ ๋‹ค์–‘ํ•œ ์•„ํ‹ฐํด ๊ธฐ๋Œ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

2๊ฐœ์˜ ์ข‹์•„์š”

๊ณ ๋ง™์Šต๋‹ˆ๋‹ค.

2๊ฐœ์˜ ์ข‹์•„์š”