Hi,
Welcome to Microsoft Q&A!
First, you need to place these five buttons in a layout panel, the layout panel in the following sample is a StackPanel named myStackPanel. Second, you could register all button click event in MainPage constructor. Third, you could use the Visual Tree to find the image inside the clicked button, then you could use image.RenderTransform = new CompositeTransform() {ScaleX=2 }
to change the scaleX
of image.
What is the most important is that you need to record the previous clicked button, so that you could set its image scaleX from 2 to 1 when you click another button.
Please refer to the following code.
public sealed partial class MainPage : Page
{
public Button oldClickButton = null;
public MainPage()
{
this.InitializeComponent();
foreach (var control in myStackPanel.Children)
{
if(control is Button)
{
var button = control as Button;
button.Click += Button_Click;
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (oldClickButton != null)
{
Image imageold = FindChild<Image>(oldClickButton);
imageold.RenderTransform = new CompositeTransform() { ScaleX = 1 };
}
Button btn = sender as Button;
Image image = FindChild<Image>(btn);
image.RenderTransform = new CompositeTransform() {ScaleX=2 };
oldClickButton = btn;
}
public T FindChild<T>(DependencyObject parent)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T typedChild)
{
return typedChild;
}
var inner = FindChild<T>(child);
if (inner != null)
{
return inner;
}
}
return default;
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.