Mvvm Sample for Winforms

Mvvm Sample for Winforms

์ด์ฒ ์šฐ

2022๋…„ 11์›”์— ์ถœ์‹œํ•œ ๋งˆ์ดํฌ๋กœ์†Œํ”„็คพ์˜ .Net 7 ์—์„œ Winforms ํ”„๋กœ์ ํŠธ๊ฐ€ MVVM ํŒจํ„ด์„ ์ง€์›ํ•˜๋„๋ก ๋ช‡ ๊ฐ€์ง€ ๋ณ€ํ™”๊ฐ€ ์žˆ์—ˆ๋‹ค.[์ฐธ๊ณ  1]

  • ButtonBase ํด๋ž˜์Šค์— Command ์†์„ฑ ์ถ”๊ฐ€
  • ToolStripItem ํด๋ž˜์Šค์— Command ์†์„ฑ ์ถ”๊ฐ€
  • Control ํด๋ž˜์Šค์— DataContext ์†์„ฑ ์ถ”๊ฐ€

์•„๋ž˜ ๊ทธ๋ฆผ์€ Winforms ๋””์ž์ธ ๋ชจ๋“œ์—์„œ Command๋ฅผ ๋ฌถ๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค€๋‹ค.[์ฐธ๊ณ  2]


(๊ทธ๋ฆผ1)

์ด ๊ธ€์—์„œ๋Š” ๊ธ€์“ฐ์ด์˜ ๋จผ์ € ๊ธ€ - MVVM Sample for Wpf[์ฐธ๊ณ  3] - ์„ ๊ธฐ์ดˆ๋กœ, .Net 8 Winforms ํ”„๋กœ์ ํŠธ์—์„œ MVVM ํŒจํ„ด์„ ๊ตฌํ˜„ํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ A๋ฅผ ๋งŒ๋“ค๊ณ  CommunityToolkit.Mvvm 8.3.2๋ฅผ ์ฐธ์กฐ๋กœ ๋„ฃ๋Š”๋‹ค.

[์ฐธ๊ณ  3]์˜ Model.cs์™€ ViewModel.cs๋ฅผ ํ”„๋กœ์ ํŠธ A์— ๋„ฃ๋Š”๋‹ค.

ํ”„๋กœ์ ํŠธ A๋ฅผ ๋งŒ๋“ค ๋•Œ ์ƒ์„ฑ๋œ Form1.cs๋ฅผ ๋‹ค์Œ์ฒ˜๋Ÿผ ๋ฐ”๊พผ๋‹ค. ์ด ๊ธ€์—์„œ๋Š” ๋ฌถ์Œ(Binding) ๊ธฐ๋Šฅ์„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

// Form1.cs

public partial class Form1 : Form
{
    private readonly ViewModel _viewModel;

    private readonly Panel _mainPanel;
    private readonly Panel _dataPanel;
    private readonly Panel _actionPanel;

    private readonly TextBox _textBoxData;
    private readonly Button _buttonReset;
    private readonly Button _buttonIncrease;

    public Form1()
    {
        InitializeComponent();

        _viewModel = new ViewModel(new Model());
        DataContext = _viewModel;

        _mainPanel = new Panel();
        _dataPanel = new Panel();
        _actionPanel = new Panel();

        _textBoxData = new TextBox();
        _buttonReset = new Button();
        _buttonIncrease = new Button();

        InitComponents();
    }

    private void InitComponents()
    {
        _mainPanel.SuspendLayout();
        SuspendLayout();

        var label = new Label
        {
            Text = "Data",
            Dock = DockStyle.Left
        };
        _textBoxData.Dock = DockStyle.Fill;
        _textBoxData.TextAlign = HorizontalAlignment.Right;
        _textBoxData.DataBindings.Add(new Binding("Text", DataContext, "Model.Data"));

        _dataPanel.Controls.Add(_textBoxData);
        _dataPanel.Controls.Add(label);

        _buttonReset.AutoSize = true;
        _buttonReset.Dock = DockStyle.Left;
        _buttonReset.Text = "Reset";
        _buttonReset.Command = _viewModel.ResetCommand;
        _buttonIncrease.AutoSize = true;
        _buttonIncrease.Dock = DockStyle.Left;
        _buttonIncrease.Text = "Increase";
        _buttonIncrease.Command = _viewModel.IncreaseCommand;

        _actionPanel.Controls.Add(_buttonReset);
        _actionPanel.Controls.Add(_buttonIncrease);

        _dataPanel.Height = 36;
        _dataPanel.Dock = DockStyle.Top;
        _actionPanel.Height = 36;
        _actionPanel.Dock = DockStyle.Top;

        _mainPanel.Controls.Add(_actionPanel);
        _mainPanel.Controls.Add(_dataPanel);

        Controls.Add(_mainPanel);
        _mainPanel.Dock = DockStyle.Fill;

        _mainPanel.ResumeLayout(false);
        _mainPanel.PerformLayout();
        ResumeLayout(false);
    }
}

(์ฝ”๋“œ 1)

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

์ด์ œ Winforms์—์„œ๋„ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง๊ณผ UI๋ฅผ WPF์ฒ˜๋Ÿผ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. Winforms์—์„œ ์ƒˆ๋กœ ๋”ํ•œ ์—ฌ๋Ÿฌ Control๋“ค๊ณผ ํ•จ๊ป˜, ์ด ๋ถ„๋ฆฌ๋ฅผ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋งŽ์€ ์ด์ ์ด ์žˆ์„ ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•œ๋‹ค. ํ•œ ์˜ˆ๋กœ Winforms ๋””์ž์ธ ๋ชจ๋“œ์—์„œ UI ์†์„ฑ์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ์€ '์žฌํ™œ์šฉโ€™์— ๋ถˆ๋ฆฌํ•œ๋ฐ, ์ด ๋ฌธ์ œ๋ฅผ ์—†์•จ ์ˆ˜ ์žˆ๋‹ค.

[์ฐธ๊ณ  1]
Windows Forms Binding Improvements in .NET 7 for MVVM Support

[์ฐธ๊ณ  2]
Whatโ€™s new in Windows Forms in .NET 7.0

[์ฐธ๊ณ  3]
MVVM Sample for Wpf (๊ณ ์นจ)

6 Likes