【问题标题】:Hide / collapse / visible dockpanel in sidebar on button click单击按钮时在侧边栏中隐藏/折叠/可见停靠面板
【发布时间】:2021-01-17 12:20:02
【问题描述】:

我正在开发侧边栏 wpf 应用程序。侧边栏有几个停靠面板。我的问题是,如何根据单击按钮在侧边栏中折叠/可见相同的停靠面板。例如:按钮 1 应该是折叠 dockPanel1 和里面的所有东西。按钮 2 应该显示它。

这是一个例子:

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="370*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0" Background="Black" x:Name="sidebarControl">
            <Grid.RowDefinitions>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <DockPanel LastChildFill="False">
                <Image DockPanel.Dock="Top" Source="{StaticResource logo}" />
                <DockPanel DockPanel.Dock="Top" Height="160" Name="dockPanel1">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="80"></RowDefinition>
                            <RowDefinition Height="80"></RowDefinition>
                        </Grid.RowDefinitions>
                        <StackPanel Grid.Row="0">
                            <Label Content="Label1"></Label>
                            <Label Content="Label2"></Label>
                        </StackPanel>
                        <StackPanel Grid.Row="1">
                            <Label Content="Label3"></Label>
                            <Label Content="Label4"></Label>
                        </StackPanel>
                    </Grid>
                </DockPanel>
            <DockPanel DockPanel.Dock="Top" Height="160" Name="dockPanel2">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="80"></RowDefinition>
                            <RowDefinition Height="80"></RowDefinition>
                        </Grid.RowDefinitions>
                        <StackPanel Grid.Row="0">
                            <!-- other content -->
                        </StackPanel>
                        <StackPanel Grid.Row="1">
                            <!-- other content -->
                        </StackPanel>
                    </Grid>
                </DockPanel>
            </DockPanel>
        </Grid>
</Grid>

【问题讨论】:

    标签: wpf xaml data-binding


    【解决方案1】:

    我使用 BooleanToVisibilityConverter 解决了这个问题。在 ViewModel 中,我放置了名为 IsVisible 的布尔属性。无论我在哪里更改我的 IsVisible 属性(设置为 true 或 false) BooleanToVisibilityConverter 类都检查它并更改停靠面板的可见性。这是我的代码:

    <DockPanel DockPanel.Dock="Top" Height="160" Name="dockPanel1" 
                               Visibility="{Binding IsVisible, Converter={StaticResource BoolToVisibility}, Mode=TwoWay}">
                       <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="80"></RowDefinition>
                                <RowDefinition Height="80"></RowDefinition>
                            </Grid.RowDefinitions>
                            <StackPanel Grid.Row="0">
                                <Label Content="Label1"></Label>
                                <Label Content="Label2"></Label>
                            </StackPanel>
                            <StackPanel Grid.Row="1">
                                <Label Content="Label3"></Label>
                                <Label Content="Label4"></Label>
                            </StackPanel>
                        </Grid>
    </DockPanel>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 2022-10-24
      • 2019-03-19
      • 2021-11-06
      • 2020-02-18
      • 1970-01-01
      相关资源
      最近更新 更多