[英]In WPF how can I control whether another button clicked
I want to determine another button is clicked in WPF. 我想确定在WPF中单击了另一个按钮。 I wrote same code as below, when I clicked that button the animation works after that when I clicked same button the animation work like as else block.
我写了下面的代码,当我单击该按钮时,动画起作用;然后,当我单击相同的按钮时,动画起作用,就像else块一样。 But I want to control if another button is clicked and than I want to work of the else block.
但是我想控制是否单击了另一个按钮,然后我要控制else块。
int i = 0;
private void btnDashboard_Click(object sender, RoutedEventArgs e)
{
// btnDashboard.Click += btnDashboard_Click;
// Button btnSender = (Button)sender;
if (i%2==0)
{
DoubleAnimation anim = new DoubleAnimation(250, 1470, new Duration(TimeSpan.FromMilliseconds(300)));
btnDashboard.BeginAnimation(FrameworkElement.WidthProperty, anim);
((ApplicationShell)App.Current.MainWindow).ShowPages(new UCDashboardIndicatorView());
i++;
}
else
{
DoubleAnimation anim = new DoubleAnimation(1470, 250, new Duration(TimeSpan.FromMilliseconds(300)));
btnDashboard.BeginAnimation(FrameworkElement.WidthProperty, anim);
base.OnApplyTemplate();
i++;
}
}
How can I control it? 我该如何控制?
Simply add your other Button
as a subscriber to the event. 只需将其他
Button
作为事件的订阅者添加即可。
<Button Click="btnDashboard_Click" ... />
<Button Click="btnDashboard_Click" ... />
And in your event method, you can get hold of the button that was clicked: 在事件方法中,您可以按住被单击的按钮:
Button btn = sender as Button;
Then, you can apply your animation to the desired button: 然后,您可以将动画应用于所需的按钮:
btn.BeginAnimation(FrameworkElement.WidthProperty, anim);
Note: The above code references btn
, which could be either one of the two buttons that was clicked . 注意:上面的代码引用了
btn
,它可以是被单击的两个按钮之一 。
If you need to take this a step further by applying an animation based on a specific button that was clicked, you could give each button a Name
and test btn.Name
in an if statement . 如果您需要通过基于被单击的特定按钮应用动画来使此步骤更进一步,则可以为每个按钮指定一个
Name
并在if语句中测试btn.Name
。
if you want to use same Click handler for two buttons, Try this: 如果要对两个按钮使用相同的Click处理程序,请尝试以下操作:
private void btnDashboard_Click(object sender, RoutedEventArgs e)
{
Button btnSender = sender as Button;
if(btnSender.Name == "btn1")
{
//Code for Button1
}
else
{
//Code for Button2
}
}
Also make sure each button has a Name property set in XAML so that you can access it in code behind. 还要确保每个按钮在XAML中都设置了Name属性,以便您可以在后面的代码中对其进行访问。 in XAML:
在XAML中:
<Grid>
<Button Name="btn1" Click="btnDashboard_Click"/>
<Button Name="btn2" Click="btnDashboard_Click"/>
</Grid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.