【问题标题】:How to change a resource templates' child value in XAML?如何在 XAML 中更改资源模板的子值?
【发布时间】:2016-08-25 16:12:52
【问题描述】:

我有一个自定义按钮Style,写在XAML。它是一个带有图像和文本的按钮。 但是Image 应该是可定制的。我需要更改设计器中的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>

我该怎么做?

【问题讨论】:

    标签: wpf xaml resourcedictionary


    【解决方案1】:

    使用方便的花花公子Tag 属性使用任意模板绑定回到属性;

    <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 的 cmets 更新以反映 wpf 中模板绑定的路径注意事项。

    【讨论】:

    • 你试过了吗?我无法让它工作。我复制粘贴了它,但它没有显示图像。我做错了吗?
    • 我在stackoverflow.com/questions/4638741/… 找到了一个相关问题,并将{TemplateBinding Tag} 代码更改为{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}。所以效果很好。如果您使用此方法或您的工作更新您的问题,我会接受它。 注意:但是这种方法只在运行时有效。该图像仅在运行时而不是设计时显示。
    • 啊,对不起,我在复制/粘贴您的模板时没有注意 x:Type,只是假设它是通用的,因为问题上没有 WPF 标签。如果您将图像的构建操作设置为资源并使用 pack uri 而不是文字文件路径,它也应该在设计时工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多