我想为应用程序中的所有切换按钮创建样式。它们具有相同的行为,除了加载不同的图像。我的想法是更改资源" toggleButtonimg_disabled"," toggleButtonimg_ismouseover"," togglebuttonimg_ispressed"one_answers" togglebuttonimg_ispressed"one_answers" togglebuttonimg_ischecked",当按下按钮时。但是它不起作用...有人可以帮我!预先感谢!
xaml
<Image Width="20" Source="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_Disabled" />
<Image Width="20" Source="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_IsMouseOver" />
<Image Width="20" Source="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_IsPressed" />
<Image Width="20" Source="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_IsChecked" />
<Style TargetType="{x:Type ToggleButton}" x:Key="ToggleButtonStyle">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Content" Value="{DynamicResource ToggleButtonImg_Disabled}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Border
x:Name="CustomControlTemplate"
CornerRadius="2"
BorderThickness="1">
<ContentPresenter
Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Content" Value="{DynamicResource ToggleButtonImg_IsMouseOver}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Content" Value="{DynamicResource ToggleButtonImg_IsPressed}" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="{DynamicResource ToggleButtonImg_IsChecked}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
代码
//按下按钮时设置按钮的图像
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(@"Resources/Images/icon_play.png", UriKind.Relative);
img.EndInit();
_receiver.Resources["ToggleButtonImg_IsChecked"] = img;
我的代码更新(仅XAML):
<Style TargetType="{x:Type ToggleButton}" x:Key="ToggleButtonStyle">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Content" Value="{DynamicResource ToggleButtonImg_Disabled}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Image x:Name="CustomControlTemplate"
Width="20"
Source="Resources/Images/icon_stop.png"
/>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="CustomControlTemplate" Property="Source" Value="{DynamicResource ToggleButtonImg_IsMouseOver}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="CustomControlTemplate" Property="Source" Value="{DynamicResource ToggleButtonImg_IsPressed}" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="CustomControlTemplate" Property="Source" Value="{DynamicResource ToggleButtonImg_IsChecked}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在您想要的控制图中添加图像控制,然后将触发器的设置器更改为:
<Setter TargetName="imageName" Property="Source" Value="{DynamicResource resourceKey}" />
编辑:而不是图像,而是使用位图像来源:
<BitmapImage Width="20" UriSource="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_Disabled" />
<BitmapImage Width="20" UriSource="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_IsMouseOver" />
<BitmapImage Width="20" UriSource="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_IsPressed" />
<BitmapImage Width="20" UriSource="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_IsChecked" />