2016-12-21 1 views
0

は(panel2 1が全領域を取る可視パネルない場合)期待どおりに動作している以下のコードパネル1のコード 以下を参照してくださいを定義WPFの2つの列(一つ一つが折りたたまれている場合に全領域を取るべきである)

しかしパネル1を折りたたむと、パネル2は親グリッドの全スペースを占めていません。

<Window x:Class="SampleApp.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:SampleApp" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <local:BoolToVisConv x:Key="boolovis" /> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="30" /> 
     </Grid.RowDefinitions>   
      <Grid Grid.Row="0"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*" /> 
       <ColumnDefinition Width="*" /> 
       </Grid.ColumnDefinitions> 
      <Grid Grid.Column="0" x:Name="panel1" Visibility="{Binding ElementName=chkbPanel1Visibiliby, Converter={StaticResource boolovis}, Path=IsChecked}"> 
       <Grid.Style> 
        <Style TargetType="Grid"> 
          <Style.Triggers> 
          <DataTrigger Binding="{Binding ElementName=panel2, Path=Visibility}" Value="Collapsed"> 
            <Setter Property="Grid.ColumnSpan" Value="2"/> 
           </DataTrigger> 
          </Style.Triggers> 
         </Style> 
        </Grid.Style> 

       <Grid Background="Yellow"> 
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="1st Panle" /> 
       </Grid> 
      </Grid> 

      <Grid Grid.Column="1" x:Name="panel2" Visibility="{Binding ElementName=chkbPanel2Visibiliby, Converter={StaticResource boolovis}, Path=IsChecked}"> 
       <Grid.Style> 
        <Style TargetType="Grid"> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding ElementName=panel1, Path=Visibility}" Value="Collapsed"> 
           <Setter Property="Grid.Column" Value="0"/> 
           <Setter Property="Grid.ColumnSpan" Value="2"/> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </Grid.Style> 
       <Grid Background="Green"> 
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="2nd Panel" /> 
       </Grid> 
      </Grid> 
      </Grid> 
     <StackPanel Grid.Row="1"> 
      <CheckBox x:Name="chkbPanel1Visibiliby" Content="Display panel 1" IsChecked="True" /> 
      <CheckBox x:Name="chkbPanel2Visibiliby" Content="Display panel 2" IsChecked="True" /> 
    </StackPanel> 
    </Grid> 
</Window> 



public class BoolToVisConv : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      return (bool)value == true ? Visibility.Visible : Visibility.Collapsed; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

ご意見、ご感想をお寄せください。

答えて

1

これは、列の定義によるものです。それはする必要があるとして、それは第二*を行い、その後、できるだけ多くの部屋を使用するように、最初の1 Autoを行います

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="30" /> 
    </Grid.RowDefinitions>   
     <Grid Grid.Row="0"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 
     <Grid Grid.Column="0" x:Name="panel1" Visibility="{Binding ElementName=chkbPanel1Visibiliby, Converter={StaticResource boolovis}, Path=IsChecked}"> 
      <Grid.Style> 

は2つの*サイズの列で何が起こっているかの良い説明のためthis previous answerを参照してください。基本的には、Autoと明示的なサイズが*よりも優先され、*のサイズが複数ある場合は、リストの順にスペースが割り当てられます。

また、その内容が縮小されている場合の可視性が崩壊するように設定されていることを確認した後、単に両方のカラムに*を指定して逃げることができるかもしれません。

関連する問題