wpf 如何更改ListView标题文本对齐方式(已使用DynamicResource设置样式时)

yruzcnhs  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(198)

我不太擅长WPF,有时候,我需要更改一些不是我开发的程序组件的设置。
我想将ListView标题从居中更改为左侧,但此ListView已使用属性Style="{DynamicResource ListViewStyle}"设置了样式。
这是ListViewStyle定义:

<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>

ScrollViewerStyle定义为:

<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>

我尝试使用下面的代码更改ListView HorizontalContentAlignment,但它将标题重置为默认样式:

<Style TargetType="{x:Type GridViewColumnHeader}">
    <Setter Property="HorizontalContentAlignment" Value="Left" />
</Style>

我该怎么办?

7nbnzgx9

7nbnzgx91#

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

<Style TargetType="{x:Type GridViewColumnHeader}" BasedOn="{StaticResource ListViewStyle}">
    <Setter Property="HorizontalContentAlignment" Value="Left" />
</Style>

请参阅the official documentation以了解有关BasedOn属性的更多信息。

相关问题