Warm tip: This article is reproduced from serverfault.com, please click

c#-如何在UWP中拉伸PathIcon

(c# - How to make PathIcon stretch in UWP)

发布于 2020-11-29 15:03:34

我想使用PathIcon与文本相同的大小,例如NavigationItem我想用<Image>它来实现它。

图像

但我想使用图标,因为它可以在深色或白色模式下自动更改颜色。

<StackPanel Orientation="Horizontal" >
    <Rectangle Width="20" Fill="BlanchedAlmond"/>
    <PathIcon Data="{StaticResource PowerPlantIcon}"/>
    <Rectangle Width="20" Fill="BlanchedAlmond"/>
    <PathIcon Data="{StaticResource PowerPlantIcon}" Height="20"/>
    <TextBlock Text="This is a Text" FontSize="20" VerticalAlignment="Center"/>
</StackPanel>

我得到这个:

图像

这是我PathIcon在App.xmal中定义的自定义

<x:String x:Key="PowerPlantIcon">
   M43,50H36.12V46.24h5.27l4.85-4.85V8.55L41.38,3.76H36.12V0H43l7,7V43ZM13.87,50H7L0,43V7L7,0h6.88V3.76H8.55L3.76,8.55V41.38l4.79,4.79h5.33Zm4.7-7.06L32.46,23.59l-7-.58L31.35,8.7H20.29L17.56,25.53l4.44.55Z
</x:String>

我发现图标大小已自动设置为50x50,无法通过设置宽度或高度进行更改。

搜索后我能找到的一切,都没有找到解决的办法PathIcon

Questioner
Akiya Xiao
Viewed
0
YanGu - MSFT 2020-11-30 15:46:27

你可以PathIcon通过将其PathIcon放入ViewBox中来调整其大小你可以将控件Width属性和Height属性绑定ViewBoxTextBlock控件以实现拉伸效果。

请检查以下代码作为参考:

<StackPanel Orientation="Horizontal" >
    <Rectangle Width="20" Fill="BlanchedAlmond"/>
    <PathIcon Data="{StaticResource PowerPlantIcon}"/>
    <Rectangle Width="20" Fill="BlanchedAlmond"/>
    
    <Viewbox Height="{Binding Path=ActualHeight,ElementName=textBlock}" Width="{Binding Path=ActualHeight,ElementName=textBlock}">
        <PathIcon Data="{StaticResource PowerPlantIcon}"/>
    </Viewbox>
    <TextBlock x:Name="textBlock" Text="This is a Text" FontSize="20" VerticalAlignment="Center"/>
</StackPanel>