Wpf 如何更改资源模板';XAML中的子值?

Wpf 如何更改资源模板';XAML中的子值?,wpf,xaml,resourcedictionary,Wpf,Xaml,Resourcedictionary,我有一个自定义按钮Style是用XAML编写的。它是一个带有图像和文本的按钮。 但是图像应该是可定制的。我需要更改designer中的Source属性 我的代码: <Window.Resources> <ResourceDictionary> <Style x:Key="SSbutton" TargetType="{x:Type Button}"> <Setter Property="Cursor" Value=

我有一个自定义按钮
Style
是用
XAML
编写的。它是一个带有图像和文本的按钮。 但是
图像
应该是可定制的。我需要更改designer中的
Source
属性

我的代码:

 <Window.Resources>
    <ResourceDictionary>
    <Style x:Key="SSbutton" TargetType="{x:Type Button}">
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Background" Value="Green"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Viewbox Stretch="Uniform">
                            <Border Background="{TemplateBinding Background}" Width="{TemplateBinding Width}"
                                Height="{TemplateBinding Height}">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center">
                                    <!--I want to change this Source property-->
                                    <Image Source="img/desktop.png" Width="30" HorizontalAlignment="Left" />
                                    <TextBlock Margin="3,0,0,0" HorizontalAlignment="Right" VerticalAlignment="Center"
                                           Text="{TemplateBinding Content}" FontSize="{TemplateBinding FontSize}"/>
                                </StackPanel>
                            </Border>
                        </Viewbox>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="25"/>
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="90" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0" Grid.Row="1" Background="LightGreen">
    <StackPanel >
        <Button Style="{StaticResource SSbutton}" Width="90" Height="30" Content="Desktop" FontSize="13"
                Foreground="White"/>
    </StackPanel>
    </Border>
</Grid>


我如何才能做到这一点?

使用带有方便的dandy
标记的任意模板绑定到属性中

<Style x:Key="SSbutton" TargetType="{x:Type Button}">
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Background" Value="Green"/>
            <!-- Set a default -->
            <Setter Property="Tag" Value="img/desktop.png"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Viewbox Stretch="Uniform">
                            <Border Background="{TemplateBinding Background}" Width="{TemplateBinding Width}"
                                Height="{TemplateBinding Height}">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center">
                                    <!--I want to change this Source property-->
                                    <Image Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Width="30" HorizontalAlignment="Left" />
                                    <TextBlock Margin="3,0,0,0" HorizontalAlignment="Right" VerticalAlignment="Center"
                                           Text="{TemplateBinding Content}" FontSize="{TemplateBinding FontSize}"/>
                                </StackPanel>
                            </Border>
                        </Viewbox>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

那么在这个情况下,

<Button Style="{StaticResource SSbutton}"
        Tag="Some/Other/Image.png"
         Width="90" Height="30" 
         Content="Desktop" 
         FontSize="13" Foreground="White"/>

希望这有帮助,干杯


编辑:根据OP的评论,更新以反映wpf中模板绑定的路径注意事项。

您尝试过吗?我不能让它工作。我做了复制粘贴,但它没有显示图像。我做错了什么吗?我在上找到了一个相关问题,并将
{TemplateBinding Tag}
代码更改为
{Binding Path=Tag,RelativeSource={RelativeSource TemplatedParent}
。所以它工作得很好。如果您使用此方法更新您的问题或您的工作,我将接受它。注意:但此方法仅在运行时有效。该图像仅在运行时显示,而不是在设计时显示。啊,很抱歉,在复制/粘贴模板时,我没有注意到x:Type,只是假设它是win universal,因为问题上没有WPF标记。如果您将映像的构建操作设置为resource,并使用包uri而不是文本文件路径,那么它也应该在设计时工作。