英文:
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:
<Style x:Key="ListViewStyle"
TargetType="{x:Type ListView}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="#121E34"/>
<Setter Property="FontFamily" Value="{DynamicResource Roboto}"/>
<Setter Property="FontSize" Value="{DynamicResource FontSize30pt}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListView}">
<Border x:Name="Border"
BorderThickness="0">
<ScrollViewer Style="{DynamicResource ScrollViewerStyle}">
<ItemsPresenter/>
</ScrollViewer>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
<Setter Property="Foreground" Value="green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="#121E34"/>
</Style>
and the ScrollViewerStyle definition is:
<Style x:Key="ScrollViewerStyle"
TargetType="{x:Type ScrollViewer}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle Style="{DynamicResource RectangleScrittura}"/>
<DockPanel>
<ScrollViewer Height="60"
Background="#121E34"
DockPanel.Dock="Top"
Focusable="false"
Foreground="#FFFFFF"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden">
<GridViewHeaderRowPresenter Height="62"
Margin="10,0"
AllowsColumnReorder="{Binding TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderContainerStyle="{Binding TemplatedParent.View.ColumnHeaderContainerStyle, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderContextMenu="{Binding TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderTemplate="{Binding TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderTemplateSelector="{Binding TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderToolTip="{Binding TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}"
Columns="{Binding TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}"
OpacityMask="{x:Null}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
TextBlock.FontFamily="{DynamicResource Roboto}"
Visibility="{Binding TemplatedParent.Visibility, RelativeSource={RelativeSource TemplatedParent}}">
</GridViewHeaderRowPresenter>
</ScrollViewer>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
CanContentScroll="True"
CanHorizontallyScroll="False"
CanVerticallyScroll="False"
KeyboardNavigation.DirectionalNavigation="Local">
</ScrollContentPresenter>
</DockPanel>
<ScrollBar x:Name="PART_VerticalScrollBar"
Grid.Column="1"
Width="40"
Margin="5,0,0,0"
BorderThickness="0"
Maximum="{TemplateBinding ScrollableHeight}"
Template="{DynamicResource ScrollBarTemplate}"
ViewportSize="{TemplateBinding ViewportHeight}"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
Value="{TemplateBinding VerticalOffset}">
</ScrollBar>
<ScrollBar x:Name="PART_HorizontalScrollBar"
Grid.Row="1"
Width="Auto"
Height="40"
Margin="0,5,0,0"
BorderThickness="0"
Maximum="{TemplateBinding ScrollableWidth}"
Template="{DynamicResource ScrollBarTemplateHorizontal}"
ViewportSize="{TemplateBinding ViewportWidth}"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
Value="{TemplateBinding HorizontalOffset}">
</ScrollBar>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I've tried to change the ListView HorizontalContentAlignment with the code below but it resets the header to the default style:
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
</Style>
What should I do?
答案1
得分: 0
要在现有样式之上应用自定义样式,请使用BasedOn
属性来继承其所有属性:
<Style TargetType="{x:Type GridViewColumnHeader}" BasedOn="{StaticResource ListViewStyle}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
</Style>
查看官方文档以了解有关BasedOn
属性的更多信息。
英文:
To apply a custom style on top of an existing one, use the BasedOn
property to inherit all of it's properties:
<Style TargetType="{x:Type GridViewColumnHeader}" BasedOn="{StaticResource ListViewStyle}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
</Style>
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.
<GridViewHeaderRowPresenter Height="62"
Margin="5,0"
HorizontalAlignment="Left"
AllowsColumnReorder="{Binding TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderContainerStyle="{Binding TemplatedParent.View.ColumnHeaderContainerStyle, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderContextMenu="{Binding TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderTemplate="{Binding TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderTemplateSelector="{Binding TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderToolTip="{Binding TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}"
Columns="{Binding TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}"
OpacityMask="{x:Null}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
TextBlock.FontFamily="{DynamicResource Roboto}"
Visibility="{Binding TemplatedParent.Visibility, RelativeSource={RelativeSource TemplatedParent}}">
</GridViewHeaderRowPresenter>
Thanks to everyone!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论