《深入浅出WPF》读书笔记之——Binding(2)

1、DataContext(数据上下文)

  • DataContext的概念允许元素从父元素继承信息来用于绑定。

  • DataContext可以直接设置为一个.NET对象,可以将DataContext设置为一个DataSourceProvider对象

  • DataSourceProvider的子类有:XmlDataProvider和ObjectDataProvider

2、为Binding指定源(Source)

如果一个Binding没有指定Source属性,则Source默认是当前控件的DataContext,如果没有显式指定当前控件的DataContext,那么直接继承父元素的DataContext

也可以直接为Binding设置Source属性,方式有以下几种

  • 通过Source属性

  • 通过RelativeSource属性

  • 通过Element属性

3、为Binding指定Source时可以使用ADO.NET数据对象(DataTable和DataView)和Linq检索的数据,DataView和Linq实现了IEnumerable接口

4、XmlDataProvider

参照《深入浅出WPF》读书笔记之——XAML篇(3)代码片段04

5、ObjectDataProvider

        <ObjectDataProvider
            x:Key="windowStyle"
            MethodName="GetValues"
            ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type Type="{x:Type WindowStyle}" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
            <ComboBox
                Grid.Column="1"
                ItemsSource="{Binding Source={StaticResource  resizeMode}}"
                SelectedValue="{Binding ResizeMode, Mode=TwoWay,  ElementName=basicWindow}" />

6、Source在xaml中的指定方法(此处无关Binding,仅讨论Source的指定方式

StaticResource

在xaml中预定义的资源(资源定义位置要在StaticResource引用之前)

在程序加载时引用资源,在程序加载时为使用StaticResource的属性赋值

查找方式:

1、首先根据Key查找当前元素的资源

2、如果不存在则根据逻辑树向上查找,直到根元素。

3、最后才查找应用程序资源(app.xaml)。

DynamicResource

可以向后引用资源

只是定义一个表达式,在程序运行时,实际调用属性时才给属性赋值

在程序运行时,引用动态资源的属性才能获得实际的值

查找方式:

1、如果你在后台代码中调用了  FindResource 或者 SetResourceReference,那么xaml中的动态资源查找会和后台指定的操作并行执行

2、首先从当前元素查找,如果当前元素包含Style属性,那么检查Style属性中的Resource Dictionary,如果当前元素包含Template属性,那么检查Template中的资源字典

3、如果不存在,则向上遍历逻辑树,直到根元素

4、检查应用程序资源(app.xaml)

5、检查资源字典(当前活动的主题),如果主题在运行时改变,动态资源的值随之改变

6、检查系统资源

 

 

如何选择

  • 你为程序创建资源的全局设计

    • 为每个Page定义单独资源

    • 在app中定义资源

    • 在松散的xaml中

    • 在仅包含资源的dll中

  • 程序的功能:是否要实时更新资源(本地化)

  • 预期的资源查找方式(向前查找 or 向后查找),静态资源仅支持向后,动态资源支持向前和向后查找。注意:此处的向前查找表示查找引用位置之后的资源,向后查找反之。msdn中的forward表示去查找那些还没有定义的资源参照:

  • A StaticResource must not attempt to make a forward reference to a resource that is defined lexically further within the XAML file. Attempting to do so is not supported, and even if such a reference does not fail, attempting the forward reference will incur a load time performance penalty when the internal hash tables representing a ResourceDictionary are searched. For best results, adjust the composition of your resource dictionaries such that forward references can be avoided. If you cannot avoid a forward reference, use DynamicResource Markup Extension instead.

  •  特定的属性、资源类型或者这些类型的原生行为(native behavior)

7、RelativeSource的使用

    //
    // Summary:
    //     描述相对于Binding 目标,Binding源的位置
    //     
    public enum RelativeSourceMode
    {
        //
        // 在一个展示数据的列表中,允许绑定到上一个数据项(不是包含数据项的控件)
        //     
        PreviousData = 0,
        //
        // 绑定到模板(包含数据绑定的控件)的父级控件(使用模板的控件)
        //     
        TemplatedParent = 1,
        //
        // 自身,自身的一个依赖属性绑定到另外一个依赖属性
        //     
        Self = 2,
        //
        // 查找父级元素,一般和AncestorType、AncestorLevel配合使用
        //     
        FindAncestor = 3
    }

PreviousData演示:

            UserList.Add(new UserInfoModel() { UserName =  "testusername1", RealName = "cat1" });
            UserList.Add(new UserInfoModel() { UserName =  "testusername2", RealName = "cat2" });
            UserList.Add(new UserInfoModel() { UserName =  "testusername3", RealName = "cat3" });
        <ListBox Grid.Row="2" ItemsSource="{Binding UserList}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding UserName}" />
                        <TextBlock Text=" : " />
                        <TextBlock Text="{Binding RealName,  RelativeSource={RelativeSource Mode=PreviousData}}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

展示效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值