如何更改ListView标题文本的对齐方式(已使用DynamicResource进行样式化)

huangapple go评论76阅读模式
英文:

How to change ListView Header text alignment (when already styled with a DynamicResource)

问题

You can try setting the HorizontalContentAlignment property for the GridViewColumnHeader within the ListViewStyle to Left like this:

<Style x:Key="ListViewStyle" TargetType="{x:Type ListView}">
    <!-- ... (existing style properties) ... -->
    
    <!-- Add this setter for GridViewColumnHeader -->
    <Setter Property="GridViewColumnHeader.HorizontalContentAlignment" Value="Left" />

    <!-- ... (existing style properties) ... -->
</Style>

This should align the header text of your ListView columns to the left without resetting the header to the default style.

英文:

I'm not very good with WPF and, sometimes, I need to change some settings of programs components I didn't develope.

I want to change a ListView headers from centered to left but this ListView is already styled with the property Style="{DynamicResource ListViewStyle}".

This is the ListViewStyle definition:

    &lt;Style x:Key=&quot;ListViewStyle&quot;
           TargetType=&quot;{x:Type ListView}&quot;&gt;
        &lt;Setter Property=&quot;SnapsToDevicePixels&quot; Value=&quot;true&quot;/&gt;
        &lt;Setter Property=&quot;OverridesDefaultStyle&quot; Value=&quot;true&quot;/&gt;
        &lt;Setter Property=&quot;ScrollViewer.HorizontalScrollBarVisibility&quot; Value=&quot;Auto&quot;/&gt;
        &lt;Setter Property=&quot;ScrollViewer.VerticalScrollBarVisibility&quot; Value=&quot;Auto&quot;/&gt;
        &lt;Setter Property=&quot;ScrollViewer.CanContentScroll&quot; Value=&quot;true&quot;/&gt;
        &lt;Setter Property=&quot;VerticalContentAlignment&quot; Value=&quot;Center&quot;/&gt;
        &lt;Setter Property=&quot;Background&quot; Value=&quot;#121E34&quot;/&gt;
        &lt;Setter Property=&quot;FontFamily&quot; Value=&quot;{DynamicResource Roboto}&quot;/&gt;
        &lt;Setter Property=&quot;FontSize&quot; Value=&quot;{DynamicResource FontSize30pt}&quot;/&gt;

        &lt;Setter Property=&quot;Template&quot;&gt;
            &lt;Setter.Value&gt;
                &lt;ControlTemplate TargetType=&quot;{x:Type ListView}&quot;&gt;
                    &lt;Border x:Name=&quot;Border&quot;
                            BorderThickness=&quot;0&quot;&gt;
                        &lt;ScrollViewer Style=&quot;{DynamicResource ScrollViewerStyle}&quot;&gt;
                            &lt;ItemsPresenter/&gt;
                        &lt;/ScrollViewer&gt;
                    &lt;/Border&gt;

                    &lt;ControlTemplate.Triggers&gt;
                        &lt;Trigger Property=&quot;IsGrouping&quot; Value=&quot;true&quot;&gt;
                            &lt;Setter Property=&quot;ScrollViewer.CanContentScroll&quot; Value=&quot;false&quot;/&gt;
                            &lt;Setter Property=&quot;Foreground&quot; Value=&quot;green&quot;/&gt;
                        &lt;/Trigger&gt;
                    &lt;/ControlTemplate.Triggers&gt;
                &lt;/ControlTemplate&gt;
            &lt;/Setter.Value&gt;
        &lt;/Setter&gt;

        &lt;Setter Property=&quot;Background&quot; Value=&quot;#121E34&quot;/&gt;
    &lt;/Style&gt;

and the ScrollViewerStyle definition is:

    &lt;Style x:Key=&quot;ScrollViewerStyle&quot;
           TargetType=&quot;{x:Type ScrollViewer}&quot;&gt;
        &lt;Setter Property=&quot;Template&quot;&gt;
            &lt;Setter.Value&gt;
                &lt;ControlTemplate TargetType=&quot;{x:Type ScrollViewer}&quot;&gt;
                    &lt;Grid&gt;
                        &lt;Grid.ColumnDefinitions&gt;
                            &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
                            &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                        &lt;/Grid.ColumnDefinitions&gt;

                        &lt;Grid.RowDefinitions&gt;
                            &lt;RowDefinition Height=&quot;*&quot;/&gt;
                            &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                        &lt;/Grid.RowDefinitions&gt;

                        &lt;Rectangle Style=&quot;{DynamicResource RectangleScrittura}&quot;/&gt;

                        &lt;DockPanel&gt;
                            &lt;ScrollViewer Height=&quot;60&quot;
                                          Background=&quot;#121E34&quot;
                                          DockPanel.Dock=&quot;Top&quot;
                                          Focusable=&quot;false&quot;
                                          Foreground=&quot;#FFFFFF&quot;
                                          HorizontalScrollBarVisibility=&quot;Hidden&quot;
                                          VerticalScrollBarVisibility=&quot;Hidden&quot;&gt;
                                &lt;GridViewHeaderRowPresenter Height=&quot;62&quot;
                                                            Margin=&quot;10,0&quot;
                                                            AllowsColumnReorder=&quot;{Binding TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}&quot;
                                                            ColumnHeaderContainerStyle=&quot;{Binding TemplatedParent.View.ColumnHeaderContainerStyle, RelativeSource={RelativeSource TemplatedParent}}&quot;
                                                            ColumnHeaderContextMenu=&quot;{Binding TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}&quot;
                                                            ColumnHeaderTemplate=&quot;{Binding TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}&quot;
                                                            ColumnHeaderTemplateSelector=&quot;{Binding TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}&quot;
                                                            ColumnHeaderToolTip=&quot;{Binding TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}&quot;
                                                            Columns=&quot;{Binding TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}&quot;
                                                            OpacityMask=&quot;{x:Null}&quot;
                                                            SnapsToDevicePixels=&quot;{TemplateBinding SnapsToDevicePixels}&quot;
                                                            TextBlock.FontFamily=&quot;{DynamicResource Roboto}&quot;
                                                            Visibility=&quot;{Binding TemplatedParent.Visibility, RelativeSource={RelativeSource TemplatedParent}}&quot;&gt;
                                &lt;/GridViewHeaderRowPresenter&gt;
                            &lt;/ScrollViewer&gt;

                            &lt;ScrollContentPresenter x:Name=&quot;PART_ScrollContentPresenter&quot;
                                                    CanContentScroll=&quot;True&quot;
                                                    CanHorizontallyScroll=&quot;False&quot;
                                                    CanVerticallyScroll=&quot;False&quot;
                                                    KeyboardNavigation.DirectionalNavigation=&quot;Local&quot;&gt;
                            &lt;/ScrollContentPresenter&gt;
                        &lt;/DockPanel&gt;

                        &lt;ScrollBar x:Name=&quot;PART_VerticalScrollBar&quot;
                                   Grid.Column=&quot;1&quot;
                                   Width=&quot;40&quot;
                                   Margin=&quot;5,0,0,0&quot;
                                   BorderThickness=&quot;0&quot;
                                   Maximum=&quot;{TemplateBinding ScrollableHeight}&quot;
                                   Template=&quot;{DynamicResource ScrollBarTemplate}&quot;
                                   ViewportSize=&quot;{TemplateBinding ViewportHeight}&quot;
                                   Visibility=&quot;{TemplateBinding ComputedVerticalScrollBarVisibility}&quot;
                                   Value=&quot;{TemplateBinding VerticalOffset}&quot;&gt;
                        &lt;/ScrollBar&gt;

                        &lt;ScrollBar x:Name=&quot;PART_HorizontalScrollBar&quot;
                                   Grid.Row=&quot;1&quot;
                                   Width=&quot;Auto&quot;
                                   Height=&quot;40&quot;
                                   Margin=&quot;0,5,0,0&quot;
                                   BorderThickness=&quot;0&quot;
                                   Maximum=&quot;{TemplateBinding ScrollableWidth}&quot;
                                   Template=&quot;{DynamicResource ScrollBarTemplateHorizontal}&quot;
                                   ViewportSize=&quot;{TemplateBinding ViewportWidth}&quot;
                                   Visibility=&quot;{TemplateBinding ComputedHorizontalScrollBarVisibility}&quot;
                                   Value=&quot;{TemplateBinding HorizontalOffset}&quot;&gt;
                        &lt;/ScrollBar&gt;
                    &lt;/Grid&gt;
                &lt;/ControlTemplate&gt;
            &lt;/Setter.Value&gt;
        &lt;/Setter&gt;
    &lt;/Style&gt;

I've tried to change the ListView HorizontalContentAlignment with the code below but it resets the header to the default style:

&lt;Style TargetType=&quot;{x:Type GridViewColumnHeader}&quot;&gt;
    &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Left&quot; /&gt;
&lt;/Style&gt;

What should I do?

答案1

得分: 0

要在现有样式之上应用自定义样式,请使用BasedOn属性来继承其所有属性:

&lt;Style TargetType=&quot;{x:Type GridViewColumnHeader}&quot; BasedOn=&quot;{StaticResource ListViewStyle}&quot;&gt;
    &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Left&quot; /&gt;
&lt;/Style&gt;

查看官方文档以了解有关BasedOn属性的更多信息。

英文:

To apply a custom style on top of an existing one, use the BasedOn property to inherit all of it's properties:

&lt;Style TargetType=&quot;{x:Type GridViewColumnHeader}&quot; BasedOn=&quot;{StaticResource ListViewStyle}&quot;&gt;
    &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Left&quot; /&gt;
&lt;/Style&gt;

See the official documentation to find out more about the BasedOn property.

答案2

得分: 0

I found an easy solution to my problem: adding HorizontalAlignment="Left" inside the GridViewHeaderRowPresenter tag of the ScrollViewerStyle definition did the trick.

英文:

I found an easy solution to my problem: adding HorizontalAlignment="Left" inside the GridViewHeaderRowPresenter tag of the ScrollViewerStyle definition did the trick.

&lt;GridViewHeaderRowPresenter Height=&quot;62&quot;
						Margin=&quot;5,0&quot;
						HorizontalAlignment=&quot;Left&quot;
						AllowsColumnReorder=&quot;{Binding TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}&quot;
						ColumnHeaderContainerStyle=&quot;{Binding TemplatedParent.View.ColumnHeaderContainerStyle, RelativeSource={RelativeSource TemplatedParent}}&quot;
						ColumnHeaderContextMenu=&quot;{Binding TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}&quot;
						ColumnHeaderTemplate=&quot;{Binding TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}&quot;
						ColumnHeaderTemplateSelector=&quot;{Binding TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}&quot;
						ColumnHeaderToolTip=&quot;{Binding TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}&quot;
						Columns=&quot;{Binding TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}&quot;
						OpacityMask=&quot;{x:Null}&quot;
						SnapsToDevicePixels=&quot;{TemplateBinding SnapsToDevicePixels}&quot;
						TextBlock.FontFamily=&quot;{DynamicResource Roboto}&quot;
						Visibility=&quot;{Binding TemplatedParent.Visibility, RelativeSource={RelativeSource TemplatedParent}}&quot;&gt;

</GridViewHeaderRowPresenter>

Thanks to everyone!

huangapple
  • 本文由 发表于 2023年4月17日 22:47:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定