WPFでInotifyPropertyChangedをどのようにバインドしますか?

WPFでは、INotifyPropertyChangedインターフェイスを実装したオブジェクトのプロパティをバインドするためにBindingクラスを使用できます。以下は、XAMLでTextBoxをINotifyPropertyChangedインターフェースを実装したViewModelクラスのプロパティにバインドする方法を示すサンプルコードです。

<Window x:Class="YourNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:YourNamespace"
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>

    <Grid>
        <TextBox Text="{Binding YourProperty, Mode=TwoWay}"/>
    </Grid>
</Window>

上記のコードでは、ViewModelクラスはINotifyPropertyChangedインターフェースを実装したViewModelクラスであり、YourPropertyという名前のプロパティがあります。次に、TextBoxのTextプロパティにバインディングを使用して、ViewModelのYourPropertyプロパティにバインドします。

ViewModelのYourPropertyプロパティが変更されると、INotifyPropertyChangedインターフェースがバインドされているUI要素(ここではTextBox)に通知され、UI画面上の対応する値が更新されます。これにより、ViewModelでYourPropertyプロパティを変更すると、UI画面にバインドされたTextBoxの値も適切に変更されます。

bannerAds