为什么设置“模板”会发生错误?打破滚动样式的属性?

发布于 2024-10-23 18:49:50 字数 1359 浏览 8 评论 0原文

我正在使用此示例来创建多列树视图,我注意到此列表视图的滚动不再正常工作:

Broken scrollbars

经过一些我发现破坏滚动条的部分是 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:

Broken scrollbars

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云淡风轻 2024-10-30 18:49:50

我认为发生这种情况是因为您编辑了 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.

兰花执着 2024-10-30 18:49:50

您需要实现自己的滚动条,因为您将覆盖默认模板。

将 ControlTemplate 的 ItemsPresenter 包装在 ScrollViewer

You need to implement your own scrollbars since you are overwriting the default template.

Wrap your ControlTemplate's ItemsPresenter in a ScrollViewer

随心而道 2024-10-30 18:49:50

最后我通过自己添加滚动条解决了这个问题 - 最初我以 nieve 的方式实现了这个,只是让原始内容滚动,我发现如果我这样做,水平滚动条就无法正常工作(标题没有不滚动)。

相反,我使用控件模板参考来弄清楚基本控件的作用并对 ListView 所做的 wjat 进行了变体。

我现在可以明白为什么当我做这样的事情时我需要设置整个模板 - 我认为很简单的实际上结果是完全特定于我的控件。

这是我的 Xaml:

<Style x:Key="{x:Static local:TreeListView.ScrollViewerStyleKey}" TargetType="ScrollViewer">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ScrollViewer">
                <Grid Background="{TemplateBinding Background}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <DockPanel Margin="{TemplateBinding Padding}">
                        <ScrollViewer DockPanel.Dock="Top"
                            HorizontalScrollBarVisibility="Hidden"
                            VerticalScrollBarVisibility="Hidden"
                            Focusable="false">
                            <GridViewHeaderRowPresenter Margin="2,0,2,0"
                                Columns="{Binding Path=TemplatedParent.Columns, 
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderContainerStyle="{Binding
                                    Path=TemplatedParent.View.ColumnHeaderContainerStyle,
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderTemplate="{Binding
                                    Path=TemplatedParent.View.ColumnHeaderTemplate,
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderTemplateSelector="{Binding 
                                    Path=TemplatedParent.View.ColumnHeaderTemplateSelector,
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                AllowsColumnReorder="{Binding
                                    Path=TemplatedParent.View.AllowsColumnReorder,
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderContextMenu="{Binding
                                    Path=TemplatedParent.View.ColumnHeaderContextMenu,
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderToolTip="{Binding
                                    Path=TemplatedParent.View.ColumnHeaderToolTip,
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </ScrollViewer>
                        <ScrollContentPresenter Name="PART_ScrollContentPresenter"
                            KeyboardNavigation.DirectionalNavigation="Local"
                            CanHorizontallyScroll="False"
                            CanVerticallyScroll="False" />
                    </DockPanel>
                    <ScrollBar Name="PART_HorizontalScrollBar"
                        Orientation="Horizontal"
                        Grid.Row="1"
                        Maximum="{TemplateBinding ScrollableWidth}"
                        ViewportSize="{TemplateBinding ViewportWidth}"
                        Value="{TemplateBinding HorizontalOffset}"
                        Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" />
                    <ScrollBar Name="PART_VerticalScrollBar"
                        Grid.Column="1"
                        Maximum="{TemplateBinding ScrollableHeight}"
                        ViewportSize="{TemplateBinding ViewportHeight}"
                        Value="{TemplateBinding VerticalOffset}"
                        Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="{x:Type local:TreeListView}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeView">
                <Border Name="Border" CornerRadius="1" BorderThickness="1">
                    <Border.BorderBrush>
                        <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
                    </Border.BorderBrush>
                    <Border.Background>
                        <SolidColorBrush Color="{DynamicResource ControlLightColor}" />
                    </Border.Background>
                    <ScrollViewer Style="{DynamicResource {x:Static local:TreeListView.ScrollViewerStyleKey}}">
                        <ItemsPresenter />
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

上面要求我向控件本身添加一些额外的属性:

public static ResourceKey ScrollViewerStyleKey
{
    get
    {
        return new ComponentResourceKey(typeof(TreeListView), "TreeListView_ScrollViewerStyleKey");
    }
}

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:

<Style x:Key="{x:Static local:TreeListView.ScrollViewerStyleKey}" TargetType="ScrollViewer">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ScrollViewer">
                <Grid Background="{TemplateBinding Background}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <DockPanel Margin="{TemplateBinding Padding}">
                        <ScrollViewer DockPanel.Dock="Top"
                            HorizontalScrollBarVisibility="Hidden"
                            VerticalScrollBarVisibility="Hidden"
                            Focusable="false">
                            <GridViewHeaderRowPresenter Margin="2,0,2,0"
                                Columns="{Binding Path=TemplatedParent.Columns, 
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderContainerStyle="{Binding
                                    Path=TemplatedParent.View.ColumnHeaderContainerStyle,
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderTemplate="{Binding
                                    Path=TemplatedParent.View.ColumnHeaderTemplate,
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderTemplateSelector="{Binding 
                                    Path=TemplatedParent.View.ColumnHeaderTemplateSelector,
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                AllowsColumnReorder="{Binding
                                    Path=TemplatedParent.View.AllowsColumnReorder,
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderContextMenu="{Binding
                                    Path=TemplatedParent.View.ColumnHeaderContextMenu,
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderToolTip="{Binding
                                    Path=TemplatedParent.View.ColumnHeaderToolTip,
                                    RelativeSource={RelativeSource TemplatedParent}}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </ScrollViewer>
                        <ScrollContentPresenter Name="PART_ScrollContentPresenter"
                            KeyboardNavigation.DirectionalNavigation="Local"
                            CanHorizontallyScroll="False"
                            CanVerticallyScroll="False" />
                    </DockPanel>
                    <ScrollBar Name="PART_HorizontalScrollBar"
                        Orientation="Horizontal"
                        Grid.Row="1"
                        Maximum="{TemplateBinding ScrollableWidth}"
                        ViewportSize="{TemplateBinding ViewportWidth}"
                        Value="{TemplateBinding HorizontalOffset}"
                        Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" />
                    <ScrollBar Name="PART_VerticalScrollBar"
                        Grid.Column="1"
                        Maximum="{TemplateBinding ScrollableHeight}"
                        ViewportSize="{TemplateBinding ViewportHeight}"
                        Value="{TemplateBinding VerticalOffset}"
                        Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="{x:Type local:TreeListView}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeView">
                <Border Name="Border" CornerRadius="1" BorderThickness="1">
                    <Border.BorderBrush>
                        <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
                    </Border.BorderBrush>
                    <Border.Background>
                        <SolidColorBrush Color="{DynamicResource ControlLightColor}" />
                    </Border.Background>
                    <ScrollViewer Style="{DynamicResource {x:Static local:TreeListView.ScrollViewerStyleKey}}">
                        <ItemsPresenter />
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The above required that I add some extra properties to the control itself:

public static ResourceKey ScrollViewerStyleKey
{
    get
    {
        return new ComponentResourceKey(typeof(TreeListView), "TreeListView_ScrollViewerStyleKey");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文