假设我们有一个带有 TextBlock 的网格。现在,如果我执行一些 RenderTransform,使 TextBlock 出现在网格之外,则 TextBlock 仍然可见。我的问题很简单:如何自动隐藏网格之外的 TextBlock 部分? (换句话说,如何使网格充当其子级的视觉边界?)
最佳答案
您可以使用与父元素的边界匹配的剪贴蒙版:
<Border Height="200" Width="200" BorderThickness="1" BorderBrush="Black" >
<Border.Clip>
<RectangleGeometry Rect="0,0,200,200"></RectangleGeometry>
</Border.Clip>
<TextBlock Text="Foo">
<TextBlock.RenderTransform>
<TranslateTransform X="180"></TranslateTransform>
</TextBlock.RenderTransform>
</TextBlock>
</Border>
在 WPF 中,您不需要手动执行此操作,只需设置 ClipToBounds="True"
关于xaml - 当子控件变换出其父网格或边框时隐藏子控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21750686/