c# - StackPanel 折叠并在按钮单击时可见

标签 c# wpf user-interface mvvm styles

我试图在单击按钮时显示一个折叠的堆栈面板,但我遇到了问题,所以我尝试扭转我的想法,我能够折叠一个可见的堆栈面板。但不幸的是,我无法实现我想要的行为,在按钮单击时显示折叠的堆栈面板。到代码: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并绑定(bind)StackPanel.VisibilityToggleButton.IsChecked属性(property)通过BooleanToVisibilityConverter转换器

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

关于c# - StackPanel 折叠并在按钮单击时可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37398233/

相关文章:

c# - 您可以将不同的 View 模型发送到 View 吗?

c# - 硬盘或硬盘的序列号

.net - 在 WPF 中实例化用户控件时 PresentationFramework.dll 中的 System.IO.IOException

java - 布局自定义按钮

c++ - 使MFC滚动条蒙皮

c# - 如何在 Windows 窗体中添加文本框验证

c# - 计算C# Windows Phone 7中的时间差

c# - 连接到 wifi 时用户界面不更新?

c# - C#中如何显示当前时间和日期

python - 来自 Process 的简单 wxPython post 事件