WPF用户控件空间折叠时不被其他人使用

时间:2019-01-29 16:12:22

标签: wpf visual-studio-2008 datagrid user-controls grid

在我的主要WPF窗口中,我具有以下分布:

<Border BorderBrush="SkyBlue"
        Background="White"
        BorderThickness="1"
        CornerRadius="20 0 0 0">

        <DockPanel>

            <Border x:Name="WindowTitleBar"  
                    DockPanel.Dock="Top"
                    CornerRadius="20 0 0 0"                                        
                    Background="LightBlue">

                    <DockPanel DockPanel.Dock="Top">

                    <!-- Here are textblocks and a status bar simulating a window title bar with maximize, minimize and close buttons -->

                    </DockPanel>                    
            </Border>

            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />    <!-- Content -->                
                    <RowDefinition Height="auto" /> <!-- Status bar at bottom -->                   
                </Grid.RowDefinitions>

                <Grid Grid.Row="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="auto" />
                    </Grid.RowDefinitions>

                    <Grid x:Name="Grid" Grid.Row="0">
                        <Grid.RowDefinitions>                           
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                        </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="0.60*" />
                            <ColumnDefinition Width="0.47*" />
                        </Grid.ColumnDefinitions>

                        <controls:MyUC_01 Grid.Row="0" Grid.Column="0"                                                
                                          Width="auto"
                                          Height="auto"
                                          DataContext="{Binding}" />

                        <controls:MyUC_02 Grid.Row="1" Grid.Column="0" 
                                          Width="auto"
                                          Height="auto"
                                          DataContext="{Binding}" />        

                        <controls:MyUC_03 Grid.Row="2" Grid.Column="0"   
                                          Width="auto"
                                          Height="auto"
                                          DataContext="{Binding}" />

                        <Expander Grid.Row="0" Grid.RowSpan="3"
                                  Grid.Column="1"       
                                  Collapsed="Expander_Collapsed"
                                  Expanded="Expander_Expanded"
                                  ExpandDirection="Left"
                                  IsExpanded="True">

                                  <!-- Expander content -->
                        </Expander>                               

                    </Grid>

                    <!-- BELOW SPACE OCCUPIED BY UCMessage KEEP BLANK WHEN VIEW MODEL PROPERTY MessageVisibility IS SET TO COLLAPSE -->
                    <controls:UCMessage Grid.Row="1" x:Name="UCMessage"
                                        Width="auto"
                                        Height="auto"
                                        DataContext="{Binding}" 
                                        Visibility="{Binding Path=MessageVisibility}" />
                </Grid>
            </Grid>

        </DockPanel>
</Border>   

当用户控件UCMessage折叠时(取决于MessageVisibility视图模型属性值),其余控件未使用其空间,并且该空间保持空白。即使调整窗口大小,此空间也不会被其他人使用。放置UCMessage的行设置为height = auto,但不起作用。

以上述分布为例:

enter image description here

1 个答案:

答案 0 :(得分:0)

在用于绑定“ MessageVisibility”的代码中,...的值设置为“ Hidden”或“ Collapsed”。它们是两件分开的事情……隐藏的意思是即使没有显示也要保留空间。除非明确需要折叠,否则不会显示且不会保留空间。

如果这不起作用,有时我注意到绑定可能不是它们出现的样子。如果您的消息绑定是绑定到主数据上下文,而不是用户控件本身的某些子级/属性,则可能需要将其包装为

<UserControl Grid.Row="1" Visibility="{Binding Path=MessageVisibility}">
    <controls:UCMessage 
        x:Name="UCMessage"
        Width="auto"
        Height="auto"
        DataContext="{Binding}" />
</UserControl>