即使不需要剪切,WPF 也会进行剪切 - 如何将其关闭?
我需要按照 ListBox.ItemTemplate
的 DataTemplate
中指定的方式从 ListBox
中浮出一些内容。我正在使用 RenderTransform,但内容在 ListBox 边界上被剪切。对于整个可视化树,ClipToBounds
为 False
。
我在某处读到,即使没有指定专用剪切属性,WPF 也会在内部执行一些剪切。我还发现使用 Canvas 有时可以解决剪切问题,但在这里没有帮助。
我怎样才能克服这个问题?这是我想要修复的一些 XAML。请注意,矩形的整个左侧部分都丢失了。
<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>
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.
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.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在解决这个问题时偶然发现了这个问题的解决方案。如果您在样式模板中将 ScrollViewer 的 HorizontalScrollMode 和 VerticalScrollMode 更改为“Disabled”,它将分别停止在每个方向上的剪切。
编辑:可能不适用于 WPF。我使用 UWP 应用进行了测试。有问题的字段是:
ScrollViewer.HorizontalScrollMode="Disabled"
ScrollViewer.VerticalScrollMode="Disabled"
I stumbled upon a solution to this problem by accident while working around it. If you change the ScrollViewer's HorizontalScrollMode and VerticalScrollMode to "Disabled" within the style template, it will stop clipping in each direction respectively.
Edit: May not work for WPF. I tested with a UWP app. The fields in question are:
ScrollViewer.HorizontalScrollMode="Disabled"
ScrollViewer.VerticalScrollMode="Disabled"
ListBoxItem
被ListBox
模板中的ScrollViewer
剪切。要解决此问题,我认为您需要从模板中删除ScrollViewer
,如果需要滚动,您可以将ListBox
包装在ScrollViewer
中>更新
Template中的
ScrollViewer
会生成一个ScrollContentPresenter
,其中又有下面的GetLayoutClip
这个类被Sealed所以你不能从它派生来覆盖这个方法。您必须实现自己的
ScrollContentPresenter
(例如MyScrollContentPresenter
),并且可能还需要实现您自己的使用MyScrollContentPresenter
的ScrollViewer
为了使这项工作有效(如果您在此方法中返回null
,我认为ListBox
边界以下的某些项目也可能变得可见)The
ListBoxItem
's are getting clipped by theScrollViewer
in theListBox
Template. To work around this I think you'll need to remove theScrollViewer
from the Template and if you need scrolling you can wrap theListBox
in aScrollViewer
Update
The
ScrollViewer
in the Template will generate aScrollContentPresenter
which in turn has the followingGetLayoutClip
This class is Sealed so you can't derive from it to override this method. You would have to implement your own
ScrollContentPresenter
(e.gMyScrollContentPresenter
) and probably your ownScrollViewer
that usesMyScrollContentPresenter
as well to make this work (and if you returnnull
in this method I think that some items below the bounds of theListBox
could become visible as well)