zoukankan      html  css  js  c++  java
  • 双向绑定 TwoWay MVVM

    1前台代码

      <Grid>
            <StackPanel >
                <Grid x:Name="gridOne">
                    <Grid.Resources>
                        <Style   TargetType="TextBlock">
                            <Setter Property="FontSize" Value="34"/>
                        </Style>
                    </Grid.Resources>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="姓名:" />
                    <TextBlock   Grid.Column="1" Text="{Binding Name}"/>
                    <TextBlock Grid.Row="1" Text="城市:" />
                    <TextBlock   Grid.Column="1" Grid.Row="1" Text="{Binding City}"/>
                </Grid>
                <Grid x:Name="gridTwp">
                    <Grid.Resources>
                        <Style   TargetType="TextBlock">
                            <Setter Property="FontSize" Value="34"/>
                        </Style>
                    </Grid.Resources>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="姓名1:" />
                    <TextBox  Name="txtName"  Grid.Column="1" Text="{Binding Name,Mode=TwoWay}"/>
                    <TextBlock Grid.Row="1" Text="城市1:" />
                    <TextBox Name="txtCity"   InputScope="ChineseFullWidth"  Grid.Column="1" Grid.Row="1" Text="{Binding City,Mode=TwoWay}"/>
                </Grid>
            </StackPanel>
        </Grid>

    2后台方法

      public class Person : INotifyPropertyChanged
            {
                public event PropertyChangedEventHandler PropertyChanged;
                protected void OnPropertyChanged([CallerMemberName] string property="")
                {
                    if (PropertyChanged!=null)
                    {
                        PropertyChanged(this, new  PropertyChangedEventArgs(property));
                    }
                }
                private string _name;
    
                public string Name
                {
                    get
                    {
                        return _name;
                    }
    
                    set
                    {
                        if (_name!=value)
                        {
                            _name = value;
                            OnPropertyChanged();
                        }
                    }
                }
                private string _city;
    
                public string City
                {
                    get
                    {
                        return _city;
                    }
    
                    set
                    {
                        if (_city != value)
                        {
                            _city = value;
                            OnPropertyChanged();
                        }
                    }
                }
    
            }

    3数据绑定

        Person ps = new Person { Name="小刘" ,City="北京" };
                gridOne.DataContext = ps;
                gridTwp.DataContext = ps;
  • 相关阅读:
    Docker swarm集群增加节点和删除节点
    docker 基础
    Mac 下 java环境 maven环境配置
    git 常用命令(含删除文件) git提交本地分支到远程分支
    github添加ssh keys
    Git在mac中和远程仓库建立连接
    初始化Git仓库(Mac环境下)
    Mac系统显示隐藏文件
    Mac:jenkins忘记管理员账号登录密码如何修改管理员账号
    java实现链表的反转
  • 原文地址:https://www.cnblogs.com/crazyair/p/4230267.html
Copyright © 2011-2022 走看看