I am a beginner and I am looking for a simple way to make a button in WPF application move when it is hovered over.
public MainWindow()
{
InitializeComponent();
}
private void btnNo_Click(object sender, RoutedEventArgs e)
{
}
I am a beginner and I am looking for a simple way to make a button in WPF application move when it is hovered over.
public MainWindow()
{
InitializeComponent();
}
private void btnNo_Click(object sender, RoutedEventArgs e)
{
}
You may put the Button
in a Grid
, and changes its Margin
in the button's MouseEnter
event, like this:
private void ChangePosition(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
var newPosition = new Thickness(10, 90, 40, 80); // assuming this is your new position
button.Margin = newPosition;
}
Assuming you want it to move back when you cease to hover over it, you can do this in pure XAML by using a style and a property trigger on the Button's IsMouseOver property. (A more typical use of style would be to create it as a resource and share it across various buttons).
<Button x:Name="button" Content="Button" Height="40" Width="60">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="RenderTransform">
<Setter.Value>
<TranslateTransform X="10" Y="10"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>