zoukankan      html  css  js  c++  java
  • WPF中使用相对资源来进行绑定,数据源是通过DataContext来指定的

    1. 最外层是Window是对象,Window的ItemsControl使用了ItemsTemplate,然后在ItemsTemplate中要绑定Language属性,

        而整个Window的数据源是通过DataContext来指定的,类型是自定义的WindowViewModel, 而Language就是WindowViewModel的一个属性

                在Window的构造函数中书写:

                   this.DataContext = new WindowViewModel();

    2.在ItemsTemplate中进行绑定,核心代码如下:

    1 <TabItem 
    2       Header="{Binding DataContext.Language, RelativeSource={RelativeSource AncestorType=Window, AncestorLevel=1}
    3                      }"  />
    View Code

      其中注意:

            使用了相对资源  RelativeSource来引用Window的DataContext,所以 AncestorType指定类型是Window, 层次是1层,
            特别要注意的是:

                  需要绑定的数据是WindowViewModel类型的,而相对资源指定的是Window元素,它是没有Language这个属性的,

          Language是WindowViewModel的属性,而在这里我们正好将WindowViewModel赋值给了Window的DataContext,

          所以在绑定时使用的Path应该是 DataContext.Language,其中DataContext是一定不能少写的

  • 相关阅读:
    Begin Again
    hadoop集群启动start-dfs.sh有节点datanode启动不了问题
    centos中python更新后yum不可用问题
    浏览器无法访问hdfs界面问题
    数据的统计分析与描述
    插值与拟合
    层次分析法
    四种规划-数学建模
    Hive深入使用
    Hadoop-Hive
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/5115355.html
Copyright © 2011-2022 走看看