WPF学习笔记三之绑定

1.绑定模式

 <TextBlock Margin="10" Text="LearningHard" Name="lbtext"
                   FontSize="{Binding ElementName=sliderFontSize, Path=Value, Mode=TwoWay}"></TextBlock>

Mode属性除了可以设置OneWay,TwoWay值外,还可以设置Default、OneTime和OneWayToSource,关于这些值更详细的介绍请自行参考MSDN:http://msdn.microsoft.com/zh-cn/library/system.windows.data.bindingmode(v=vs.110).aspx。

2.绑定元素对象

TextBlock的FontSize属性的值来自与Slider控件的Value值

这里Path除了可以直接绑定属性之外,还可以绑定属性的属性,如FontFamily.Source,也可以指向属性使用的索引器,如Content.Children[0]。当然你也可以执行多层次的路径,如指向属性的属性的属性等。

<StackPanel>
        <Slider Name="sliderFontSize" Margin="3" Minimum="1" Maximum="40" Value="10" TickFrequency="1" TickPlacement="TopLeft"/>
        <TextBlock Margin="10" Text="LearningHard" Name="lbtext" FontSize="{Binding ElementName=sliderFontSize, Path=Value}"></TextBlock>
</StackPanel>

3.绑定更新

当使用OneWay或TwoWay绑定时,改变后的值会立即从源传播到目标。对于滑动条,然而,从目标到源传播未必会立即发生。因为,它们的行为是由Binding.UpdateSourceTrigger属性控制,该属性可以使用下图列出的某个值。注意,UpdateSourceTrigger属性值并不影响目标的更新方式,它仅仅控制TwoWay模式或OneWayToSource模式的绑定更新源的方式。而文本框正是使用LostFocus方式从目标向源进行更新的。

  <StackPanel Orientation="Horizontal" Margin="5">
            <TextBlock VerticalAlignment="Center">Set FontSize:</TextBlock>
            <TextBox Text="{Binding ElementName=lbtext, Path=FontSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
  </StackPanel>

4.绑定非元素对象

当绑定一个非元素对象时,不能使用Binding.ElementName属性,但可以使用以下属性中的一个:

Source——该属性是指向源对象的引用,即提供数据的对象。
RelativeSource——该属性使用RelativeSource对象指定绑定源的相对位置,默认值为null。
DataContext属性——如果没有使用Source或RelativeSource属性指定一个数据源,WPF会从当前元素开始在元素树中向上查找。检查每个元素的DataContext属性,并使用第一个非空的DataContext属性。当然你也可以自己设置DataContext属性。

数据源还可以是XAML文件,ADO.NET数据对象、集合等

using System.ComponentModel;

namespace WPFBindingDemo
{
    public class Student:INotifyPropertyChanged
    {
        private int m_ID;
        private string m_StudentName;
        private double m_Score;
       
        public int ID
        {
            get { return m_ID; }
            set 
            {
                if (value != m_ID)
                {
                    m_ID = value;
                    Notify("ID");
                }
            }
        }

        public string StudentName
        {
            get { return m_StudentName; }
            set
            {
                if (value != m_StudentName)
                {
                    m_StudentName = value;
                    Notify("StudentName");
                }
            }
        }

        public double Score
        {
            get { return m_Score; }
            set 
            {
                if (value != m_Score)
                {
                    m_Score = value;
                    Notify("Score");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void Notify(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

5.UI虚拟化

UI虚拟化是列表仅为当前显示项创建容器对象的一种技术,例如,如果有一个具有5万条记录的列表,但是可见区域只能包含30条记录,ListBox控件只创建30个ListBoxItem对象。如果ListBox控件不支持UI虚拟化的话,它将需要生成全部5万个ListBoxItem对象,这显然需要占用更多的内存。并且分配这些对象的时间用户明显可以感觉到,这就带来了非常不好的用户体验。WPF中UI虚拟化是通过VirtualizingStackPanel容器实现的。像ListBox、ListView和DataGrid都自动使用VirtualizingStackPanel面板布局它们的子元素,所以,这些控件都默认支持虚拟化功能。然而,ComboBox需要支持虚拟化支持,必须明确提供新的ItemPanelTemplate添加虚拟化支持

猜你喜欢

转载自www.cnblogs.com/jasonlai2016/p/12287035.html