为什么设置“模板”会发生错误?打破滚动样式的属性?
我正在使用此示例来创建多列树视图,我注意到此列表视图的滚动不再正常工作:
经过一些我发现破坏滚动条的部分是 TreeListView 的“Template”属性的设置:
<Style TargetType="{x:Type l:TreeListView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type l:TreeListView}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<DockPanel>
<GridViewHeaderRowPresenter Columns="{StaticResource gvcc}" DockPanel.Dock="Top"/>
<ItemsPresenter/>
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
注释掉上面的内容可以修复滚动条(但显然意味着不会显示网格列标题)。事实上,我发现即使下面的模板也会破坏滚动条:
<Style TargetType="{x:Type l:TreeListView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type l:TreeListView}">
<ItemsPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这是为什么?
I'm using this sample to create a multi-column tree view and I've noticed that scrolling no longer works correctly for this list view:
After some playing around I've discovered that the bit that is breaking the scrollbars is the setting of the "Template" property for the TreeListView:
<Style TargetType="{x:Type l:TreeListView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type l:TreeListView}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<DockPanel>
<GridViewHeaderRowPresenter Columns="{StaticResource gvcc}" DockPanel.Dock="Top"/>
<ItemsPresenter/>
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Commenting out the above fixes the scrollbars (however obviously means that the grid column headers are not shown). In fact I've discovered that even the following template breaks the scrollbars:
<Style TargetType="{x:Type l:TreeListView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type l:TreeListView}">
<ItemsPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Why is this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为发生这种情况是因为您编辑了 WPF 定义的控件模板。
它被定义为有一个滚动条。
您覆盖该模板并且不添加模板。
我对此不是 100% 确定,但我想知道为什么你一开始就搞乱控制模板?
也许您要编辑的是 DataTemplate?
DataTemplate 决定如何呈现由数据绑定绑定的对象。
I think this happens because you edit the template of the control defined by WPF.
and it's defined to have a scrollBar.
you override that template and don't add one.
i'm not 100% sure about that, but i'm wondering why are you messing with the Control Template to begin with?
maybe what you want to edit is the DataTemplate?
The DataTemplate decides how to present the object that is bound by Data Binding.
您需要实现自己的滚动条,因为您将覆盖默认模板。
将 ControlTemplate 的
ItemsPresenter
包装在ScrollViewer
中You need to implement your own scrollbars since you are overwriting the default template.
Wrap your ControlTemplate's
ItemsPresenter
in aScrollViewer
最后我通过自己添加滚动条解决了这个问题 - 最初我以 nieve 的方式实现了这个,只是让原始内容滚动,我发现如果我这样做,水平滚动条就无法正常工作(标题没有不滚动)。
相反,我使用控件模板参考来弄清楚基本控件的作用并对
ListView
所做的 wjat 进行了变体。我现在可以明白为什么当我做这样的事情时我需要设置整个模板 - 我认为很简单的实际上结果是完全特定于我的控件。
这是我的 Xaml:
上面要求我向控件本身添加一些额外的属性:
In the end I solved this by adding in the scrollbars myself - initially I implemented this the nieve way and just got the original content to scroll, the I discovered that the horizontal scroll bar didn't work properly if I did that (the headers didn't scroll).
Instead I used the Control Template Reference to figure out what the base control does and did a variation on wjat the
ListView
does.I can see now why I need to set the entire template when I do things like this - what I believed was simple actually turned out to be completely specific to my control.
Here is my Xaml:
The above required that I add some extra properties to the control itself: