堆栈面板折叠并在单击按钮时可见



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

<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>选项卡中的 Visibility 属性的优先级高于样式或触发器中设置的任何属性,因此永远不会应用触发器。有关更多详细信息,请参阅依赖项属性优先级列表。

若要修复当前解决方案,请将 Visibliity 属性移出 <StackPanel> 标记并移入样式,如下所示:

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

也就是说,我个人会推荐一些类似 切换按钮 StackPanel.Visibility绑定到ToggleButton.IsChecked ,就像这个答案所暗示的那样。

我解决了将孩子设置为空

的问题
stackPanel.Children.Clear();

如果您需要第一次显示/隐藏面板,则此方法有效,如果需要运行时

则不起作用

像 Stackpanel 一样简单.Visibility = Visibility.Collapsed.

相关内容

  • 没有找到相关文章

最新更新