Ond*_*cek 31
这是我个人喜欢在大多数时间使用的另一种解决方案,如果我想要一个带有命令的图像而不将其封闭在另一个控件中.
<Image Source="Images/tick.png" Cursor="Hand" Tooltip="Applies filter">
<Image.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding ApplyFilter, Mode=OneTime}" />
</Image.InputBindings>
</Image>
Run Code Online (Sandbox Code Playgroud)
我设置了它的属性Hand,Tooltip因此它更清楚它是一个动作而不是一个静态图像.
Tho*_*que 20
您需要将图像放在一个按钮中,并将按钮绑定到命令:
<Button Command="{Binding MyCommand}">
<Image Source="myImage.png" />
</Button>
Run Code Online (Sandbox Code Playgroud)
如果您不想要标准按钮镶边,只需更改按钮的模板:
<ControlTemplate x:Key="tplFlatButton" TargetType="{x:Type Button}">
<Border Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
TextElement.Foreground="{TemplateBinding Foreground}"
TextElement.FontFamily="{TemplateBinding FontFamily}"
TextElement.FontSize="{TemplateBinding FontSize}"
TextElement.FontStretch="{TemplateBinding FontStretch}"
TextElement.FontWeight="{TemplateBinding FontWeight}"/>
</Border>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
请注意,您还需要更改其他属性以覆盖默认按钮样式,否则上面的模板将使用默认按钮背景和边框:
<Style x:Key="stlFlatButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Template" Value="{StaticResource tplFlatButton}" />
</Style>
Run Code Online (Sandbox Code Playgroud)
Rob*_*ney 12
避免使用按钮并使用Hyperlink替代方法可能更简单:
<TextBlock DockPanel.Dock="Top">
<Hyperlink Command="{Binding SomeCommand}">
<Image Source="image.png" />
</Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
请注意,这将使用默认文本修饰呈现超链接,因此您需要添加一个删除它的样式 - 将其放入包含元素的资源字典中将会起到作用:
<Style x:Key={x:Type Hyperlink}" TargetType="Hyperlink">
<Setter Property="TextDecorations"
Value="{x:Null}" />
</Style>
Run Code Online (Sandbox Code Playgroud)
@Robert Rossney的简化版答案:
<TextBlock>
<Hyperlink Command="{Binding SomeCommand}" TextDecorations="{x:Null}">
<Image Source="{StaticResource image.png}" Width="16" />
</Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
包含图像的最佳方法是使用a {StaticResource x},查看WPF图像资源
| 归档时间: |
|
| 查看次数: |
17786 次 |
| 最近记录: |