温馨提示×

WPF中inotifypropertychanged怎么绑定

wpf
小亿
83
2024-02-18 09:46:19
栏目: 编程语言

在WPF中,您可以使用Binding类来绑定一个实现了INotifyPropertyChanged接口的对象的属性。下面是一个示例代码,演示了如何在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的属性。然后,我们在TextBoxText属性上使用Binding来绑定到ViewModelYourProperty属性上。

ViewModel中的YourProperty属性发生变化时,INotifyPropertyChanged接口会通知绑定的UI元素(这里是TextBox),从而更新UI界面上对应的值。这样,在ViewModel中修改YourProperty属性时,UI界面上绑定的TextBox的值也会相应地变化。

0