[英]Change the size of an image inside border on click in c#
How can I make an image inside button to make it bigger when I click it using Width
and Height
. 当我使用
Width
和Height
单击按钮时,如何在按钮内部制作图像以使其更大。
My Button XAML code is this: 我的Button XAML代码是这样的:
<Button x:Name="Input2" Grid.Row="0" MouseEnter="Input2_MouseEnter" MouseLeave="Input2_MouseLeave" Click="Input2_Click" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}">
<Button.Template>
<ControlTemplate>
<Border HorizontalAlignment="Center" VerticalAlignment="Center" >
<Image Source= "C:\input.png"
Width="40"
Height="40"/>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
Input2.Height
- Change the size of the button, not the image inside it. Input2.Height
更改按钮的大小,而不是按钮内的图像。
My C# code: 我的C#代码:
private void Input2_Click(object sender, RoutedEventArgs e)
{
// What to do here?
}
Instead of attaching a Click event handler, better use a Trigger on the Button's IsPressed
property: 与其附加Click事件处理程序,不如对Button的
IsPressed
属性使用触发器:
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<Border>
<Image x:Name="image" Width="40" Height="40" Source="C:\input.png"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="image" Property="Width" Value="50"/>
<Setter TargetName="image" Property="Height" Value="50"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
The simplest solution will be to add x:Name="input2Image" to the Image element, and enlarge the image using that name, like that: 最简单的解决方案是将x:Name =“ input2Image”添加到Image元素,并使用该名称放大图像,如下所示:
<Button x:Name="Input2" Grid.Row="0" MouseEnter="Input2_MouseEnter" MouseLeave="Input2_MouseLeave" Click="Input2_Click" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}">
<Button.Template>
<ControlTemplate>
<Border HorizontalAlignment="Center" VerticalAlignment="Center" >
<Image x:Name="Input2Image" Source= "C:\input.png"
Width="40"
Height="40"/>
</Border>
</ControlTemplate>
</Button.Template>
And: 和:
private void Input2_Click(object sender, RoutedEventArgs e)
{
this.Input2Image.Height = 80;
this.Input2Image.Width = 80;
}
This should work, but you might consider start using Bindings and the MVVM pattern, to maximize the power of WPF. 这应该可行,但是您可以考虑开始使用绑定和MVVM模式,以最大化WPF的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.