zoukankan      html  css  js  c++  java
  • wp中的双向绑定

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WpfApplication2
    {
        /// <summary>
        /// 继承INotifyPropertyChanged接口,当值发生改变时,向客户端发出通知。
        /// </summary>
        public class SliderClass : INotifyPropertyChanged
        {
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            private double _length=0.0;
            public double Length
            {
                get
                {
                    return _length;
                }
                set
                {
                    _length = value;
                    NotifyPropertyChanged("Length");
                }
            }
        }
    }

    在mainpage中定义两个Slider。

        <Grid x:Name="grid">
            <Grid.RowDefinitions>
                <RowDefinition Height="120*"/>
                <RowDefinition Height="199*"/>
            </Grid.RowDefinitions>
            <Slider Value="{Binding Path=Length,Mode=TwoWay}" Margin="10,0,-10,71"/>
            <Slider Value="{Binding Path=Length,Mode=TwoWay}" Grid.Row="1" Margin="0,47,0,123"></Slider>
        </Grid>

    在后台new一个SliderClass作为Grid的数据源。会发现第一个Slider会带动第二个Slider变化,反之也成立。

    本文主要理解INotifyPropertyChanged接口和实现双向绑定基础以及如何调用NotifyPropertyChanged。

  • 相关阅读:
    2015-3-27
    2015-3-26
    2015-3-25
    2015-3-24
    oracle数据字典表 系统表
    PowerDesigner教程系列(六)概念数据模型
    PowerDesigner教程系列(五)概念数据模型
    PowerDesigner教程系列(四)概念数据模型
    PowerDesigner教程系列(三)概念数据模型
    PowerDesigner教程系列(二)概念数据模型
  • 原文地址:https://www.cnblogs.com/lzhp/p/3209355.html
Copyright © 2011-2022 走看看