[英]WPF button icon change programmatically
I want to change icon of the following button programmatically 我想以编程方式更改以下按钮的图标
<Button x:Name="btnSendInvite" Height="32" Background="Transparent" BorderBrush="Transparent"
BorderThickness="0" Cursor="Hand" Margin="1,5" ToolTip="Call" Click="btnSendInvite_Click" >
<Button.Template>
<ControlTemplate TargetType="Button">
<DockPanel>
<DockPanel.Background>
<ImageBrush ImageSource="Resources/button.png" />
</DockPanel.Background>
<StackPanel Orientation="Horizontal">
<Image Source="Resources/dial.png" Height="30" Margin="14,0"></Image>
</StackPanel>
</DockPanel>
</ControlTemplate>
</Button.Template>
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Need to change dial.png
icon to hangup.png
. 需要将dial.png
图标更改为hangup.png
。 How it possible in c# 在C#中怎么可能
If you absolutely have to change the image using code, then you should data bind the Image.Source
property and then change your data bound image path: 如果绝对必须使用代码来更改图像,则应将数据绑定到Image.Source
属性,然后更改数据绑定的图像路径:
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageSource}" Height="30" Margin="14,0" />
</StackPanel>
The default value for your new property would be Resources/dial.png
and you could change it like so: 您的新属性的默认值是Resources/dial.png
,您可以像这样更改它:
ImageSource = "Resources/hangup.png";
Of course, your ImageSource
property must notify the INotifyPropertyChanged
Interface of the change so that the UI can update the Image
. 当然,您的ImageSource
属性必须将此更改通知INotifyPropertyChanged
接口 ,以便UI可以更新Image
。 However, it must be said that using a Trigger
or DataTrigger
to change the Image.Source
in XAML would be a better solution. 但是,必须说在XAML中使用Trigger
或DataTrigger
更改Image.Source
将是更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.