-1

我创建了一个 ResourceDictionary 以便ControlTemplateButton.

这是我的 XAML 代码

<ControlTemplate TargetType="{x:Type Button}" x:Key="BoutonRessources">
<Button Width="32" Margin="0,0,7,0" Name="tbrClear" ToolTip="Clear" VerticalAlignment="Center" BorderThickness="0" HorizontalAlignment="Center" Background="Transparent" HorizontalContentAlignment="Center">
    <Button.Content>
        <Border>
            <Image Source="xRtDiva_XWPF_TBR_PREMIER.PNG_IMAGES.png" Height="18"/>
            <Border.Style>
                <Style TargetType="{x:Type Border}">
                    <Setter Property="Background" Value="Transparent" />
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <ImageBrush ImageSource="BoutonToolbarSelected.png"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Height"  Value="22"/>
                            <Setter Property="Width"  Value="32"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
        </Border>
    </Button.Content>
    <Button.Style>
        <Style TargetType="{x:Type Button}" >
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}" >
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
</Button>
</ControlTemplate>

当我在我的 XAML 代码上使用 y 模板时:

<Button Template="{StaticResource BoutonRessources}">

我想给出一个参数,以便为这个模板“BoutonRessources”设置图片。

我试图像这样修改我的代码:

<Button Template="{StaticResource BoutonRessources}">
   <Button.Content>
       <Border>
           <Image Source="myNewPicture.png" Height="18"/>
       </Border>
   </Button.Content>
</Button>

请问如何个性化我的按钮模板的图片?

非常感谢 :)

4

2 回答 2

0

在您的按钮模板中,您已将按钮的内容设置为固定值,因此在按钮本身上分配内容将无济于事。在模板的某处,您需要添加一个ContentPresenter. 如果您这样做,ContentPresenter将被您分配的按钮内容所取代。

你想达到什么目的?你想让按钮有 2 个图像还是 1 个图像?也许看看这个关于创建模板的教程(或其他许多模板之一),看看它是否有帮助: http: //mark-dot-net.blogspot.com/2007/07/creating-custom-wpf-button-模板-in.html

举个简单的例子,对模板的这个小修改会将您添加到内容中的图像添加到您在模板中手动分配的图像旁边。请注意我是如何添加 StackPanel 并将 a 放置ContentPresenter在模板中的。这将替换为您分配给按钮的内容,并将显示在您在模板中设置的“xRtDiva...”图像的右侧:

<ControlTemplate TargetType="{x:Type Button}" x:Key="BoutonRessources">
        <Button Width="50" Margin="126.5,126,115.5,126" Name="tbrClear" ToolTip="Clear" VerticalAlignment="Center" BorderThickness="0" HorizontalAlignment="Center" Background="Transparent" HorizontalContentAlignment="Center">
            <StackPanel Orientation="Horizontal">
                <Border>
                    <Image Source="xRtDiva_XWPF_TBR_PREMIER.PNG_IMAGES.png" Height="18"/>
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Setter Property="Background" Value="Transparent" />
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <ImageBrush ImageSource="BoutonToolbarSelected.png"/>
                                        </Setter.Value>
                                    </Setter>
                                    <Setter Property="Height"  Value="22"/>
                                    <Setter Property="Width"  Value="32"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                </Border>
                <Border>
                    <ContentPresenter/>
                </Border>
            </StackPanel>

            <Button.Style>
                <Style TargetType="{x:Type Button}" >
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}" >
                                <ContentPresenter />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Button.Style>

        </Button>
    </ControlTemplate>

但是,我对Button.Style您在按钮内分配的内容感到非常困惑。这样做的目的是什么?

于 2013-02-21T21:14:50.023 回答
0

为什么你不能只做以下事情?

<Button>
    <Image Source="firstImage.png" />
</Button>

<Button>
    <Image Source="secondImage.png" />
</Button>

<Button>
    <Image Source="thirdImage.png" />
</Button>

在这种情况下,如果您想更改按钮模板,只需将 ContentPresenter 元素放在您希望图像所在的位置。

于 2013-02-21T21:57:59.917 回答