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

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

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:

  1. <Style x:Key="ListViewStyle" TargetType="{x:Type ListView}">
  2. <!-- ... (existing style properties) ... -->
  3. <!-- Add this setter for GridViewColumnHeader -->
  4. <Setter Property="GridViewColumnHeader.HorizontalContentAlignment" Value="Left" />
  5. <!-- ... (existing style properties) ... -->
  6. </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:

  1. &lt;Style x:Key=&quot;ListViewStyle&quot;
  2. TargetType=&quot;{x:Type ListView}&quot;&gt;
  3. &lt;Setter Property=&quot;SnapsToDevicePixels&quot; Value=&quot;true&quot;/&gt;
  4. &lt;Setter Property=&quot;OverridesDefaultStyle&quot; Value=&quot;true&quot;/&gt;
  5. &lt;Setter Property=&quot;ScrollViewer.HorizontalScrollBarVisibility&quot; Value=&quot;Auto&quot;/&gt;
  6. &lt;Setter Property=&quot;ScrollViewer.VerticalScrollBarVisibility&quot; Value=&quot;Auto&quot;/&gt;
  7. &lt;Setter Property=&quot;ScrollViewer.CanContentScroll&quot; Value=&quot;true&quot;/&gt;
  8. &lt;Setter Property=&quot;VerticalContentAlignment&quot; Value=&quot;Center&quot;/&gt;
  9. &lt;Setter Property=&quot;Background&quot; Value=&quot;#121E34&quot;/&gt;
  10. &lt;Setter Property=&quot;FontFamily&quot; Value=&quot;{DynamicResource Roboto}&quot;/&gt;
  11. &lt;Setter Property=&quot;FontSize&quot; Value=&quot;{DynamicResource FontSize30pt}&quot;/&gt;
  12. &lt;Setter Property=&quot;Template&quot;&gt;
  13. &lt;Setter.Value&gt;
  14. &lt;ControlTemplate TargetType=&quot;{x:Type ListView}&quot;&gt;
  15. &lt;Border x:Name=&quot;Border&quot;
  16. BorderThickness=&quot;0&quot;&gt;
  17. &lt;ScrollViewer Style=&quot;{DynamicResource ScrollViewerStyle}&quot;&gt;
  18. &lt;ItemsPresenter/&gt;
  19. &lt;/ScrollViewer&gt;
  20. &lt;/Border&gt;
  21. &lt;ControlTemplate.Triggers&gt;
  22. &lt;Trigger Property=&quot;IsGrouping&quot; Value=&quot;true&quot;&gt;
  23. &lt;Setter Property=&quot;ScrollViewer.CanContentScroll&quot; Value=&quot;false&quot;/&gt;
  24. &lt;Setter Property=&quot;Foreground&quot; Value=&quot;green&quot;/&gt;
  25. &lt;/Trigger&gt;
  26. &lt;/ControlTemplate.Triggers&gt;
  27. &lt;/ControlTemplate&gt;
  28. &lt;/Setter.Value&gt;
  29. &lt;/Setter&gt;
  30. &lt;Setter Property=&quot;Background&quot; Value=&quot;#121E34&quot;/&gt;
  31. &lt;/Style&gt;

and the ScrollViewerStyle definition is:

  1. &lt;Style x:Key=&quot;ScrollViewerStyle&quot;
  2. TargetType=&quot;{x:Type ScrollViewer}&quot;&gt;
  3. &lt;Setter Property=&quot;Template&quot;&gt;
  4. &lt;Setter.Value&gt;
  5. &lt;ControlTemplate TargetType=&quot;{x:Type ScrollViewer}&quot;&gt;
  6. &lt;Grid&gt;
  7. &lt;Grid.ColumnDefinitions&gt;
  8. &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
  9. &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
  10. &lt;/Grid.ColumnDefinitions&gt;
  11. &lt;Grid.RowDefinitions&gt;
  12. &lt;RowDefinition Height=&quot;*&quot;/&gt;
  13. &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
  14. &lt;/Grid.RowDefinitions&gt;
  15. &lt;Rectangle Style=&quot;{DynamicResource RectangleScrittura}&quot;/&gt;
  16. &lt;DockPanel&gt;
  17. &lt;ScrollViewer Height=&quot;60&quot;
  18. Background=&quot;#121E34&quot;
  19. DockPanel.Dock=&quot;Top&quot;
  20. Focusable=&quot;false&quot;
  21. Foreground=&quot;#FFFFFF&quot;
  22. HorizontalScrollBarVisibility=&quot;Hidden&quot;
  23. VerticalScrollBarVisibility=&quot;Hidden&quot;&gt;
  24. &lt;GridViewHeaderRowPresenter Height=&quot;62&quot;
  25. Margin=&quot;10,0&quot;
  26. AllowsColumnReorder=&quot;{Binding TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}&quot;
  27. ColumnHeaderContainerStyle=&quot;{Binding TemplatedParent.View.ColumnHeaderContainerStyle, RelativeSource={RelativeSource TemplatedParent}}&quot;
  28. ColumnHeaderContextMenu=&quot;{Binding TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}&quot;
  29. ColumnHeaderTemplate=&quot;{Binding TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}&quot;
  30. ColumnHeaderTemplateSelector=&quot;{Binding TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}&quot;
  31. ColumnHeaderToolTip=&quot;{Binding TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}&quot;
  32. Columns=&quot;{Binding TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}&quot;
  33. OpacityMask=&quot;{x:Null}&quot;
  34. SnapsToDevicePixels=&quot;{TemplateBinding SnapsToDevicePixels}&quot;
  35. TextBlock.FontFamily=&quot;{DynamicResource Roboto}&quot;
  36. Visibility=&quot;{Binding TemplatedParent.Visibility, RelativeSource={RelativeSource TemplatedParent}}&quot;&gt;
  37. &lt;/GridViewHeaderRowPresenter&gt;
  38. &lt;/ScrollViewer&gt;
  39. &lt;ScrollContentPresenter x:Name=&quot;PART_ScrollContentPresenter&quot;
  40. CanContentScroll=&quot;True&quot;
  41. CanHorizontallyScroll=&quot;False&quot;
  42. CanVerticallyScroll=&quot;False&quot;
  43. KeyboardNavigation.DirectionalNavigation=&quot;Local&quot;&gt;
  44. &lt;/ScrollContentPresenter&gt;
  45. &lt;/DockPanel&gt;
  46. &lt;ScrollBar x:Name=&quot;PART_VerticalScrollBar&quot;
  47. Grid.Column=&quot;1&quot;
  48. Width=&quot;40&quot;
  49. Margin=&quot;5,0,0,0&quot;
  50. BorderThickness=&quot;0&quot;
  51. Maximum=&quot;{TemplateBinding ScrollableHeight}&quot;
  52. Template=&quot;{DynamicResource ScrollBarTemplate}&quot;
  53. ViewportSize=&quot;{TemplateBinding ViewportHeight}&quot;
  54. Visibility=&quot;{TemplateBinding ComputedVerticalScrollBarVisibility}&quot;
  55. Value=&quot;{TemplateBinding VerticalOffset}&quot;&gt;
  56. &lt;/ScrollBar&gt;
  57. &lt;ScrollBar x:Name=&quot;PART_HorizontalScrollBar&quot;
  58. Grid.Row=&quot;1&quot;
  59. Width=&quot;Auto&quot;
  60. Height=&quot;40&quot;
  61. Margin=&quot;0,5,0,0&quot;
  62. BorderThickness=&quot;0&quot;
  63. Maximum=&quot;{TemplateBinding ScrollableWidth}&quot;
  64. Template=&quot;{DynamicResource ScrollBarTemplateHorizontal}&quot;
  65. ViewportSize=&quot;{TemplateBinding ViewportWidth}&quot;
  66. Visibility=&quot;{TemplateBinding ComputedHorizontalScrollBarVisibility}&quot;
  67. Value=&quot;{TemplateBinding HorizontalOffset}&quot;&gt;
  68. &lt;/ScrollBar&gt;
  69. &lt;/Grid&gt;
  70. &lt;/ControlTemplate&gt;
  71. &lt;/Setter.Value&gt;
  72. &lt;/Setter&gt;
  73. &lt;/Style&gt;

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

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

What should I do?

答案1

得分: 0

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

  1. &lt;Style TargetType=&quot;{x:Type GridViewColumnHeader}&quot; BasedOn=&quot;{StaticResource ListViewStyle}&quot;&gt;
  2. &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Left&quot; /&gt;
  3. &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:

  1. &lt;Style TargetType=&quot;{x:Type GridViewColumnHeader}&quot; BasedOn=&quot;{StaticResource ListViewStyle}&quot;&gt;
  2. &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Left&quot; /&gt;
  3. &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.

  1. &lt;GridViewHeaderRowPresenter Height=&quot;62&quot;
  2. Margin=&quot;5,0&quot;
  3. HorizontalAlignment=&quot;Left&quot;
  4. AllowsColumnReorder=&quot;{Binding TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}&quot;
  5. ColumnHeaderContainerStyle=&quot;{Binding TemplatedParent.View.ColumnHeaderContainerStyle, RelativeSource={RelativeSource TemplatedParent}}&quot;
  6. ColumnHeaderContextMenu=&quot;{Binding TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}&quot;
  7. ColumnHeaderTemplate=&quot;{Binding TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}&quot;
  8. ColumnHeaderTemplateSelector=&quot;{Binding TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}&quot;
  9. ColumnHeaderToolTip=&quot;{Binding TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}&quot;
  10. Columns=&quot;{Binding TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}&quot;
  11. OpacityMask=&quot;{x:Null}&quot;
  12. SnapsToDevicePixels=&quot;{TemplateBinding SnapsToDevicePixels}&quot;
  13. TextBlock.FontFamily=&quot;{DynamicResource Roboto}&quot;
  14. 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:

确定