上一篇转载了RelativeSource的三种用法,其中第二种用法较常见,这里记录一下项目中曾经发生错误的地方,以防自己哪天忘记了,又犯了同样错误—WPF RelativeSource属性-优快云博客
先回顾一下:
控件关联其父级容器的属性——AncestorType
详细介绍下AncestorLevel,它指的是以Bingding目标控件为起点的层级偏移量,S1的偏移量是1,G2的偏移量是2,G1是偏移量3,AncestorType指的是要找的目标对象的类型。值得注意的是AncestorLevel必须参考AncestorType使用,如上面设置了AncestorType={x:Type Grid},则Bingding在寻找时会忽略非Grid的控件,此时G2的偏移量是1,G1的偏移量是2,StackPanel被忽略。
错误代码:
<TabControl>
<TabItem
Width="150"
Height="30"
Header="TabItem1"
IsSelected="True">
<ScrollViewer MaxHeight="{Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabItem, AncestorLevel=1}}">
<Grid>

文章讲述了在WPF项目中使用RelativeSource的错误案例,特别是在设置ScrollViewerMaxHeight时,由于误将AncestorType设为TabItem导致显示不全。作者提醒需确保AncestorType正确指向TabControl,以确保控件正确渲染。
最低0.47元/天 解锁文章
949

被折叠的 条评论
为什么被折叠?



