C# 如何为堆栈面板实现点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17698723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement a click event for a stackpanel
提问by Nii Laryea
I checked the stackpanel class here http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.aspxand it has no click event.
我在这里检查了堆栈面板类http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.aspx并且它没有单击事件。
I'm working on a windows phone 8 app and I've got a textbox and some buttons on a stack panel. I want to include a feature where the stackpanel can be clicked then the visibility of the controls on it are set to collapsed, and then when clicked again they become visible.
我正在开发 Windows Phone 8 应用程序,并且在堆栈面板上有一个文本框和一些按钮。我想包含一个功能,在该功能中可以单击堆栈面板,然后将其上的控件的可见性设置为折叠,然后再次单击时它们变得可见。
How do I do this?
我该怎么做呢?
采纳答案by Uma Shankar Pathak
You can solve this problem in a little tricky manner, if it is good then it's ok otherwise i'll Post the another one.
你可以用一个有点棘手的方式来解决这个问题,如果它很好,那么没关系,否则我会发布另一个。
<StackPanel Background="Red" MinHeight="80" VerticalAlignment="Top" Tap="StackPanel_Tap_1" Orientation="Horizontal">
<Button x:Name="btn1" Content="Button"/>
<Button x:Name="btn2" Content="Button"/>
<TextBox Height="72" x:Name="textbox1" TextWrapping="Wrap" Text="TextBox" Width="456"/>
</StackPanel>
private void StackPanel_Tap_1(object sender, GestureEventArgs e)
{
if (btn1.IsEnabled==false)
{
btn1.IsEnabled = true;
btn1.Visibility = Visibility.Visible;
btn2.Visibility = Visibility.Visible;
textbox1.Visibility = Visibility.Visible;
}
else
{
btn1.IsEnabled = false;
btn1.Visibility = Visibility.Collapsed;
btn2.Visibility = Visibility.Collapsed;
textbox1.Visibility = Visibility.Collapsed;
}
}
回答by Daniel Hilgarth
Try using the MouseLeftButtonUp
event.
尝试使用该MouseLeftButtonUp
事件。
回答by Tommy
put the StackPanel
inside the Border
control, use MouseLeftButtonUp
of the Border
to handle event and set background of the Border
to #000001
把StackPanel
内部Border
控制,使用MouseLeftButtonUp
的的Border
的处理事件,并设置背景Border
,以#000001
回答by Jan Peter
You could probably use the TouchUp
and TouchDown
event.
But I think you have to check if the TouchDown
is on the same StackPanel
as the TouchUp
. So you can check if it was a "click".
您可能可以使用TouchUp
andTouchDown
事件。但我认为你必须检查是否TouchDown
是同一StackPanel
的TouchUp
。所以你可以检查它是否是“点击”。
回答by Jon B
You could just wrap the whole stackpanel in a button:
您可以将整个堆栈面板包装在一个按钮中:
<button>
<stackpanel>
</stackpanel>
</button>
Then attach a click event or command to the button as you see fit.
然后在您认为合适的情况下将单击事件或命令附加到按钮。