首页 > 解决方案 > 重新设计 UWP 工具包扩展器箭头

问题描述

我正在尝试从 Windows 社区工具包中重新设置 Expander 控件的样式,将默认的“>”符号更改为实心箭头(有点像播放按钮)。按照这个问题中接受的答案,我从 Community Toolkit github repo 复制了默认样式,并将其粘贴到 ResourceDictionary(名为 CustomStyles.xaml)中,然后将FontIcon Glyph属性从更改。我在该Page.Resources部分中引用了该 ResourceDictionary,然后将新样式绑定到 Expander 的HeaderTemplate属性并带有StaticResource引用。一切都应该正常工作,但有些事情导致它失败。

抛出的异常也无济于事,因为错误会显示消息Failed to assign to property 'Microsoft.Toolkit.Uwp.UI.Controls.HeaderedContentControl.HeaderTemplate'. The text associated with this error code could not be found.

有人可以帮忙吗?

主页.xaml

<Page
x:Class="SharpFTP.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SharpFTP"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="using:Microsoft.Toolkit.Uwp.UI.Controls"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="CustomStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="3*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto" MinHeight="40" />
    </Grid.RowDefinitions>
    <StackPanel x:Name="itemsPanel" Grid.Column="0" Grid.Row="0">
        <toolkit:Expander Header="Favorites" ExpandDirection="Down" HeaderTemplate="{StaticResource expanderFullArrowStyle}">

        </toolkit:Expander>
        <TextBlock Text="Favorites" />
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Connected to " Style="{ThemeResource SubtitleTextBlockStyle}" Foreground="LightGray" />
            <TextBlock Text="     10.3.12.128" Style="{ThemeResource SubtitleTextBlockStyle}" Foreground="White"/>
        </StackPanel>
        <AppBarSeparator Width="{Binding ActualWidth, ElementName=itemsPanel}" Height="2" Foreground="LightGray" />
    </StackPanel>
</Grid>

资源字典 XAML

我只包含了修改后的部分,因为完整的样式有 300 行长。完整的默认扩展器样式可以在这里找到

    <FontIcon x:Name="Arrow" Margin="12" FontFamily="Segoe MDL2 Assets" FontSize="12"
              Glyph="&#xE937;" RenderTransformOrigin="0.5,0.5">
              <FontIcon.RenderTransform>
                      <RotateTransform />
              </FontIcon.RenderTransform>
    </FontIcon>

标签: c#uwpresourcedictionarywindows-community-toolkit

解决方案


您的错误的原因应该是类型不匹配。

您修改了 Expender 的默认样式,则expanderFullArrowStyle不应在HeaderTemplate. 请试试这个:

<toolkit:Expander Header="Favorites" ExpandDirection="Down" Style="{StaticResource expanderFullArrowStyle}">
</toolkit:Expander>

推荐阅读