当前位置: 首页 > news >正文

WPF RelativeSource属性-目标对象类型易错

上一篇转载了RelativeSource的三种用法,其中第二种用法较常见,这里记录一下项目中曾经发生错误的地方,以防自己哪天忘记了,又犯了同样错误—WPF RelativeSource属性-CSDN博客

先回顾一下:

控件关联其父级容器的属性——AncestorType

详细介绍下AncestorLevel,它指的是以Bingding目标控件为起点的层级偏移量,S1的偏移量是1,G2的偏移量是2,G1是偏移量3,AncestorType指的是要找的目标对象的类型。值得注意的是AncestorLevel必须参考AncestorType使用,如上面设置了AncestorType={x:Type Grid},则Bingding在寻找时会忽略非Grid的控件,此时G2的偏移量是1,G1的偏移量是2,StackPanel被忽略。

错误代码:

<TabControl><TabItemWidth="150"Height="30"Header="TabItem1"IsSelected="True"><ScrollViewer MaxHeight="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabItem, AncestorLevel=1}}"><Grid><Grid.RowDefinitions><RowDefinition /></Grid.RowDefinitions><Grid Grid.Row="0"><TextBlockHorizontalAlignment="Center"VerticalAlignment="Center"FontSize="100"Text="用户控件" /></Grid></Grid></ScrollViewer></TabItem>
</TabControl>

Page界面显示效果:文字显示不全,只显示了高度为30的文字部分

问题出在这句话:<ScrollViewer MaxHeight="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabItem, AncestorLevel=1}}">

AncestorType=TabItem,往上查找第一个TabItem控件,其Height="30",这样ScrollViewer的MaxHeight=30,直接造成显示不全

改前台代码AncestorType=TabControl,如下

<TabControl><TabItemWidth="150"Height="30"Header="TabItem1"IsSelected="True"><ScrollViewer MaxHeight="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabControl, AncestorLevel=1}}"><Grid><Grid.RowDefinitions><RowDefinition /></Grid.RowDefinitions><Grid Grid.Row="0"><TextBlockHorizontalAlignment="Center"VerticalAlignment="Center"FontSize="100"Text="用户控件" /></Grid></Grid></ScrollViewer></TabItem>
</TabControl>

Page界面显示效果:文字显示完整,所以使用RelativeSource查找目标类型 AncestorType时,一定要仔细!

http://www.lryc.cn/news/214966.html

相关文章:

  • Java while 和do while 循环
  • 应用软件安全编程--03净化传递给 Runtime.exec() 方法的非受信数据
  • uniapp阻止冒泡的方法,点击事件嵌套点击事件,怎么阻止同时触发
  • 【云原生基础】了解云原生,什么是云原生?
  • Android.bp探究
  • 【LeetCode】415 字符串相加
  • 【RP-RV1126】配置一套简单的板级配置
  • 解决uniapp的video标签和transition属性使用时出现错位的问题
  • 电脑校园杂志电脑校园杂志社电脑校园编辑部2023年第9期目录
  • NSSCTF做题第十页(1)
  • 文件详细操作过程(C语言)
  • python使用ffmpeg来制作音频格式转换工具(优化版)
  • Debug技巧-不启用前端访问后端
  • 由CAB/PAB展开的一些思考
  • 系列十五、idea全局配置
  • 高德Go生态建设与研发实践
  • SpringCloud中Turbine 1.X版本BUG
  • SCSS的用法有哪些?分别举例
  • Spring controller层请求数据和响应数据的简单方法
  • 中国商界杂志中国商界杂志社中国商界编辑部2023年第10期目录查询
  • 记录 vue + vuetify + electron 安装过程
  • 短视频账号矩阵系统saas源码搭建/技术
  • 词向量模型Word2Vec
  • 公会发展计划(GAP):经过实战考验的 Web3 任务模式
  • 网络工程师基础知识(2)
  • 创建ABAP数据库表和ABAP字典对象-理解表字段02
  • 2021-arxiv-GPT Understands, Too
  • 【Spark】What is the difference between Input and Shuffle Read
  • redis相关的一些面试题?
  • 什么是Babel?它的主要作用是什么?