先看一段代码
1 <Style.Triggers> 2 <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorLevel=1,AncestorType=ScrollViewer},Path=ComputedVerticalScrollBarVisibility}" Value="Visible"> 3 <Setter Property="Width" Value="586"/> 4 <Setter Property="HorizontalAlignment" Value="Left"/> 5 </DataTrigger> 6 </Style.Triggers>
假如想根据ScrollViewer的滚动条是否显示来设置触发器Trigger,需要注意的是:
可以使用数据触发器DataTrigger,接下来的就是关键了,如果你查看SrollViewer的源码,
你就会发现SrollViewer有好几个与滚动条显示状态相关的属性,如下:
Computed [kəm'pju:tid] v. 计算(compute的过去式)
1 //我们需要绑定的是这两个属性中的某一个,为只读属性 2 public Visibility ComputedHorizontalScrollBarVisibility { get; } 3 public Visibility ComputedVerticalScrollBarVisibility { get; } 4 //以下两个属性是用来设置滚动条的显示状态的 5 public ScrollBarVisibility HorizontalScrollBarVisibility { get; set; } 6 public ScrollBarVisibility VerticalScrollBarVisibility { get; set; }
另外官方文档也有相关说明:https://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.computedverticalscrollbarvisibility.aspx