在MVVM架构中,界面View的数据源来自ViewModel,如果想实现 从ViewModel到View 和 从View到ViewModel的双向交互,这里有一种基于MVVM框架下的方式,即使用RaisePropertyChanged,当然还有一种基于INotifyPropertyChanged接口的方式实现,这里我们主要讲下RaisePropertyChanged 实现双向交互的方式。----姜彦20180117

 

1.View

<Window x:Class="MVVM_Binding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:MVVM_Binding.ViewModel"        
        Title="MainWindow" 
        Height="350" 
        Width="525">
    <Grid x:Name="grid1" >
        
        <TextBox Height="23" 
                 HorizontalAlignment="Left" 
                 Margin="71,42,0,0" 
                 Name="textBox1" 
                 VerticalAlignment="Top" 
                 Width="120" 
                 Text="{Binding Path= Name,Mode=TwoWay}"
                 />
        <TextBox Height="23" 
                 HorizontalAlignment="Left" 
                 Margin="71,81,0,0" 
                 Name="textBox2" 
                 VerticalAlignment="Top" 
                 Width="120" 
                 Text="{Binding Path= Char,Mode=TwoWay}"
                 />
        <TextBox Height="23" 
                 HorizontalAlignment="Left" 
                 Margin="71,123,0,0" 
                 Name="textBox3" 
                 VerticalAlignment="Top" 
                 Width="120" 
                 Text="{Binding Path= Len}"
                 />
        <Button Content="测试" 
                Height="23" 
                HorizontalAlignment="Left" 
                Margin="221,42,0,0" 
                Name="button1" 
                VerticalAlignment="Top" 
                Width="75" Click="button1_Click" />
        <Button Content="测试2" 
                Height="23" 
                HorizontalAlignment="Left" 
                Margin="221,78,0,0" 
                Name="button2" 
                VerticalAlignment="Top" 
                Width="75" Click="button2_Click" />
    </Grid>
</Window>
View Code

相关文章:

  • 2021-09-05
  • 2022-12-23
  • 2021-08-08
  • 2021-11-21
猜你喜欢
  • 2021-08-20
  • 2022-02-09
  • 2021-09-13
  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
相关资源
相似解决方案