How to implement a click event for a stackpanel

给你一囗甜甜゛ 提交于 2019-12-10 14:14:55

问题


I checked the stackpanel class here http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.aspx and it has no click event.

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.

How do I do this?


回答1:


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;
        }

    }



回答2:


Try using the MouseLeftButtonUp event.




回答3:


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.




回答4:


put the StackPanel inside the Border control, use MouseLeftButtonUp of the Border to handle event and set background of the Border to #000001




回答5:


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".



来源:https://stackoverflow.com/questions/17698723/how-to-implement-a-click-event-for-a-stackpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!