「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.04.21
XML
カテゴリ: C#.NET


MVVM(Model-View-ViewModel)パターンは、C#WPFアプリケーションの開発でよく使用されます。このパターンは、アプリケーションをモデル、ビュー、ビューモデルの3つの部分に分割します。モデルはアプリケーションのデータやビジネスロジックを表し、ビューはユーザーインターフェースを表します。そして、ビューモデルはモデルからデータを受け取り、ビューに表示するためのデータを保持し、ビューからのユーザーの操作を受け取り、それをモデルに伝達します。


以下に、基本的なMVVMパターンのサンプルコードを示します。




```csharp

public class PersonModel

{

    public string Name { get; set; }

    public int Age { get; set; }

}

```


### ViewModel(ビューモデル)


```csharp

using System.ComponentModel;


public class PersonViewModel : INotifyPropertyChanged

{

    private PersonModel _person;


    public event PropertyChangedEventHandler PropertyChanged;


    public PersonViewModel()

    {

        _person = new PersonModel();

    }


    public string Name

    {

        get { return _person.Name; }

        set

        {

            if (_person.Name != value)

            {

                _person.Name = value;

                OnPropertyChanged(nameof(Name));

            }

        }

    }


    public int Age

    {

        get { return _person.Age; }

        set

        {

            if (_person.Age != value)

            {

                _person.Age = value;

                OnPropertyChanged(nameof(Age));

            }

        }

    }


    protected virtual void OnPropertyChanged(string propertyName)

    {

        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }

}

```


### View(ビュー)


XAMLを使用してビューを作成します。


```xml

<Window x:Class="MVVMSample.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:MVVMSample"

        mc:Ignorable="d"

        Title="MVVM Sample" Height="250" Width="350">


    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="Auto"/>

        </Grid.RowDefinitions>


        <Label Content="Name:"/>

        <TextBox Grid.Row="0" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>


        <Label Grid.Row="1" Content="Age:"/>

        <TextBox Grid.Row="1" Text="{Binding Age, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />


        <Button Grid.Row="2" Content="Save" Command="{Binding SaveCommand}"/>

    </Grid>

</Window>

```


### MainWindow(Viewのコードビハインド)


```csharp

using System.Windows;


namespace MVVMSample

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

            DataContext = new PersonViewModel();

        }

    }

}

```


この例では、PersonModelがデータを表し、PersonViewModelがビューに表示するためのデータを保持します。ビュー(MainWindow.xaml)はPersonViewModelにバインドされており、ユーザーがデータを変更するとViewModelがそれを処理し、必要に応じてModelに変更を反映します。






お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.04.21 14:30:57


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

© Rakuten Group, Inc.
Mobilize your Site
スマートフォン版を閲覧 | PC版を閲覧
Share by: