StackPanel 折叠并在按钮单击时可见 [英] StackPanel Collapsed and Visible on Button Click

查看:70
本文介绍了StackPanel 折叠并在按钮单击时可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在单击按钮时显示一个折叠的堆栈面板,但我遇到了问题,所以我尝试扭转我的想法,我能够折叠一个可见的堆栈面板.但不幸的是,我无法实现我想要的行为,在单击按钮时显示折叠的堆栈面板.到代码:D

I am trying to show one collapsed stackpanel on button click, but I'm having problems so I tried reverse my thoughts and I was able to collapse an visible stackpanel. But unfortunately I was unable to implement the behavior I want, show an collapsed stack panel on button click. To the code :D

XAML

<Button x:Name="sentButton" Content="Add Friend" Style="{DynamicResource FlatButtonStyle}" Margin="493,0,0,0" HorizontalAlignment="Left" Width="106"/>
    <StackPanel Style="{DynamicResource stackCollapsed}" Visibility="Collapsed">
        <Label Content="Invite Friends" FontWeight="Bold" Margin="0,0,477,0" Height="32" />
        <StackPanel Orientation="Horizontal" Margin="26,0,0,0">
            <Label Content="Enter your friend's email" Width="222" Height="25" />
            <TextBox Text="{Binding Email, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource MyTextBox}" x:Name="textBoxEmail" Width="298"/>
            <Button x:Name="button1" Content="Send" Command="{Binding AddCommand}" Width="77" Style="{DynamicResource FlatButtonStyle}" Margin="20,0,0,0"/>
        </StackPanel>
    </StackPanel>

样式

<!-- Style Collapsed-->
<Style x:Key="stackCollapsed" TargetType="StackPanel">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=sentButton,Path=IsPressed}" Value="true">
            <Setter Property="StackPanel.Visibility" Value="Visible"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

推荐答案

代替 Button 使用 ToggleButton 并将 StackPanel.Visibility 绑定到 ToggleButton.IsChecked 属性通过 BooleanToVisibilityConverter 转换器

Instead of Button use ToggleButton and bind StackPanel.Visibility to ToggleButton.IsChecked property via BooleanToVisibilityConverter converter

<ToggleButton x:Name="sentButton" Content="Add Friend" Margin="493,0,0,0" HorizontalAlignment="Left" Width="106"/>
<StackPanel Visibility="{Binding ElementName=sentButton, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}">
    <Label Content="Invite Friends" FontWeight="Bold" Margin="0,0,477,0" Height="32" />
    <StackPanel Orientation="Horizontal" Margin="26,0,0,0">
        <Label Content="Enter your friend's email" Width="222" Height="25" />
        <TextBox Text="{Binding Email, UpdateSourceTrigger=PropertyChanged}"  x:Name="textBoxEmail" Width="298"/>
        <Button x:Name="button1" Content="Send" Command="{Binding AddCommand}" Width="77" Margin="20,0,0,0"/>
    </StackPanel>
</StackPanel>

其中转换器定义如下

<Window.Resources>
    <ResourceDictionary>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </ResourceDictionary>
</Window.Resources>

这篇关于StackPanel 折叠并在按钮单击时可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆