C# 参数和资源字典
我创建了一个ResourceDictionary,以便为C# 参数和资源字典,c#,wpf,xaml,controltemplate,C#,Wpf,Xaml,Controltemplate,我创建了一个ResourceDictionary,以便为按钮创建一个控制模板 这是我的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" Hori
按钮
创建一个控制模板
这是我的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}">
<Button Template="{StaticResource BoutonRessources}">
<Button.Content>
<Border>
<Image Source="myNewPicture.png" Height="18"/>
</Border>
</Button.Content>
</Button>
我想给出一个参数,以便设置此模板“BoutonResources”的图片
我试图像这样修改代码:
<Button Template="{StaticResource BoutonRessources}">
<Button Template="{StaticResource BoutonRessources}">
<Button.Content>
<Border>
<Image Source="myNewPicture.png" Height="18"/>
</Border>
</Button.Content>
</Button>
我如何个性化我的按钮模板的图片请
非常感谢:)在您的按钮模板中,您已将按钮的内容设置为固定值,因此在按钮上分配内容本身不会起任何作用。在模板的某个地方,您需要添加一个
ContentPresenter
。如果执行此操作,ContentPresenter
将被指定按钮内容的内容所取代
你想完成什么?您希望按钮具有2个图像还是1个图像?也许可以看看这篇关于创建模板的教程(或其他教程之一),看看它是否有帮助:
作为一个简单的示例,对模板的这一微小修改会将您添加的图像添加到模板中手动指定的图像旁边的内容中。注意我是如何添加StackPanel并在模板中放置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>
<Image Source="firstImage.png" />
</Button>
<Button>
<Image Source="secondImage.png" />
</Button>
<Button>
<Image Source="thirdImage.png" />
</Button>
在这种情况下,如果要更改按钮模板,只需将ContentPresenter元素放在您希望图像的位置。在什么条件下要显示不同的图像?图像是您希望按钮显示的唯一内容,还是希望显示文本等其他内容?仅图像:)我希望能够将=“xRtDiva\u XWPF\u TBR\u PREMIER.PNG\u IMAGES.PNG”更改为“myPicture.PNG”,如我的示例:)您好,我想要什么?我只想能够更改“xRtDiva_XWPF_TBR_PREMIER.PNG_IMAGES.PNG”:)所以您希望它默认为xRtDiva吗。。。然后如果你在按钮上设置内容,它会覆盖默认值吗?如果您只是想创建一个具有(可自定义)内容图像的按钮,我建议您看看这个答案:不,它不会覆盖默认值。你能给我举个例子吗,我不明白你提供给我的答案链接:(非常感谢:)我仍然不清楚你想做什么。您是否只需要一个显示图像的按钮?如果是这样,就不需要使用ControlTemplate
。不要指定模板并将图像
设置为按钮的内容(与您之前的评论一样,但删除模板
)。我想要一个带有图像和鼠标上背景图像的按钮。我创建了一个模板,因为我需要更改多个案例的图像,我不想复制所有案例的所有代码。请告诉我在模板上插入ContentPresenter的位置好吗?非常感谢:)