[英]Multiple ToggleButton image with different highlight image in WPF
I am very new to WPF and needed some pointers as to why this is not working correctly. 我对WPF还是很陌生,因此需要一些关于为什么它无法正常工作的提示。
I am trying to make a maximize button that will change to a restore button when clicked. 我试图制作一个最大化按钮,当单击该按钮时将变为还原按钮。 I thought a toggle button with 2 different styles that would be changed in the code behind could work.
我认为具有2种不同样式的切换按钮可以在后面的代码中进行更改。 I am first trying to get the maximize button working and have ran into a problem.
我首先尝试使最大化按钮正常工作,但遇到了问题。 I get the error 'System.Windows.Controls.Image' is not a valid value for the 'System.Windows.Controls.Image.Source' property on a Setter.
我在设置器上收到错误“ System.Windows.Controls.Image”对于“ System.Windows.Controls.Image.Source”属性的无效值。 in my xaml.
在我的xaml中。 I seem to be not understanding something completely.
我似乎没有完全理解某些东西。 Any help would be most helpful :)
任何帮助将是最有帮助的:)
Ryan 瑞安
<Style x:Key="Maximize" TargetType="{x:Type ToggleButton}">
<Style.Resources>
<Image x:Key="MaxButtonImg" Source="/Project;component/Images/maxbutton.png" />
<Image x:Key="MaxButtonHighlight" Source="/Project;component/Images/maxbutton-highlight.png" />
</Style.Resources>
<Setter Property="ContentTemplate">
<Setter.Value>
<Image>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{DynamicResource MaxButtonImg}"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="{DynamicResource MaxButtonHighlight}"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Setter.Value>
</Setter>
</Style>
<ToggleButton Name="MaxButton" Width="31" Height="31" BorderThickness="0" Click="MaxButton_Click" Margin="0,0,10,0" Tag="Max"
Style="{DynamicResource Maximize}" />
My code behind would do something simple like this: 我后面的代码将执行以下简单操作:
private void MaxButton_Click(object sender, RoutedEventArgs e)
{
ToggleButton tg = (ToggleButton)sender;
if ( tg.IsChecked == true) {
tg.Style = (Style)FindResource("Restore");
this.WindowState = WindowState.Maximized;
} else {
tg.Style = (Style)FindResource("Maximize");
this.WindowState = WindowState.Normal;
}
}
You don't want to change the image on mouse over. 您不想在鼠标悬停时更改图像。 I added my images to a folder called Images in the project and set build action on the images to Resource.
我将图像添加到项目中一个名为Images的文件夹中,并将对图像的构建操作设置为Resource。
<Window.Resources>
<Image x:Key="minImage" Source="/Images/min.png" Height="16" Width="16" />
<Image x:Key="maxImage" Source="/Images/max.png" Height="16" Width="16" />
<Style TargetType="{x:Type ToggleButton}" x:Key="minMaxButtonStyle">
<Setter Property="Content" Value="{DynamicResource minImage}" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="{DynamicResource maxImage}" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<ToggleButton Style="{StaticResource minMaxButtonStyle}" />
</StackPanel>
</Window>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.