即使不需要剪辑,WPF 剪辑 - 如何将其关闭? [英] WPF clipping even when no clipping is desired - how to turn it off?

查看:24
本文介绍了即使不需要剪辑,WPF 剪辑 - 如何将其关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 ListBox 中浮出一些内容,如 DataTemplate 中为 ListBox.ItemTemplate 指定的那样.我正在使用 RenderTransform 但内容在 ListBox 边界上被裁剪.ClipToBounds 对于整个可视化树来说是 False.

I need to float out some content out of the ListBox as specified in a DataTemplate for an ListBox.ItemTemplate. I am using RenderTransform but the content gets clipped on ListBox boundaries. ClipToBounds is False for the entire visual tree.

我在某处读到 WPF 内部会执行一些剪辑,即使没有指定专用剪辑属性.我还发现使用 Canvas 有时可以解决剪辑问题,但在这里无济于事.

I have read somewhere that WPF internally performs some clipping even when none is specified with dedicated clipping properties. I have also found out that using Canvas can sometimes cure the clipping problem but it does not help here.

我怎样才能克服这个问题?这是我想要修复的一些 XAML.请注意矩形的整个左侧部分缺失.

How can I overcome this problem? Here is some XAML that I want to fix. Please note the entire left part of rectangle is missing.

    <ListBox>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Rectangle Fill="Red" Stroke="Green" StrokeThickness="4" Width="100" Height="50">
                    <Rectangle.RenderTransform>
                        <TranslateTransform X="-50" />
                    </Rectangle.RenderTransform>
                </Rectangle>
            </DataTemplate>
        </ListBox.ItemTemplate>

        42
    </ListBox>

推荐答案

ListBoxItem 正在被 ListBox 中的 ScrollViewer 剪裁> 模板.要解决此问题,我认为您需要从模板中删除 ScrollViewer,如果需要滚动,您可以将 ListBox 包装在 ScrollViewer 中>

The ListBoxItem's are getting clipped by the ScrollViewer in the ListBox Template. To work around this I think you'll need to remove the ScrollViewer from the Template and if you need scrolling you can wrap the ListBox in a ScrollViewer

<ScrollViewer HorizontalScrollBarVisibility="Auto"
              VerticalScrollBarVisibility="Auto">
    <ListBox Margin="100,10,0,0">
        <ListBox.Template>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true">
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ListBox.Template>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Rectangle Fill="Red" Stroke="Green" StrokeThickness="4" Width="100" Height="50">
                    <Rectangle.RenderTransform>
                        <TranslateTransform X="-50" />
                    </Rectangle.RenderTransform>
                </Rectangle>
            </DataTemplate>
        </ListBox.ItemTemplate> 42
    </ListBox>
</ScrollViewer>

更新

模板中的 ScrollViewer 将生成一个 ScrollContentPresenter,它依次具有以下 GetLayoutClip

The ScrollViewer in the Template will generate a ScrollContentPresenter which in turn has the following GetLayoutClip

protected override Geometry GetLayoutClip(Size layoutSlotSize)
{
    return new RectangleGeometry(new Rect(base.RenderSize));
}

这个类是密封的,所以你不能从它派生来覆盖这个方法.您必须实现自己的 ScrollContentPresenter(例如 MyScrollContentPresenter),也可能实现自己的使用 MyScrollContentPresenterScrollViewer使这项工作(如果您在此方法中返回 null,我认为 ListBox 边界以下的某些项目也可能变得可见)

This class is Sealed so you can't derive from it to override this method. You would have to implement your own ScrollContentPresenter (e.g MyScrollContentPresenter) and probably your own ScrollViewer that uses MyScrollContentPresenter as well to make this work (and if you return null in this method I think that some items below the bounds of the ListBox could become visible as well)

这篇关于即使不需要剪辑,WPF 剪辑 - 如何将其关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆