WPF扩展器单击事件上的扩展/折叠网格列

时间:2019-01-20 18:44:32

标签: wpf mvvm visual-studio-2008 .net-3.5 expander

我有MVVM应用程序。在主窗口中,我将网格放在2列以下。在第2列中,我放置了WPF扩展器,并将其默认设置为折叠状态(IsExpanded =“ False”)。当应用程序执行时,我希望网格列1填充网格的整个宽度,并且网格列2填充宽度0(折叠)。因此,当我单击扩展器时,我希望网格列2扩展为0.47 *的宽度。然后,如果我再次单击扩展器,我希望网格列1填充网格的整个宽度,并且网格列2可以折叠(宽度= 0)。我怎样才能做到这一点?下面的代码不起作用。

<Grid x:Name="Grid">
<Grid.RowDefinitions>            
    <RowDefinition Height="47.5*" />           
    <RowDefinition Height="auto" />            
</Grid.RowDefinitions>

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

<controls:UCIndicationsOfUse Grid.Row="0" Grid.Column="0"
                             HorizontalAlignment="Stretch"
                             VerticalAlignment="Stretch"
                             Width="auto"
                             Height="auto"
                             DataContext="{Binding}" />

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

<Expander Grid.Row="0" 
          Grid.RowSpan="2"
          Grid.Column="1"  
          Width="25"            
          ExpandDirection="Left"
          IsExpanded="False"
          HorizontalAlignment="Right">
    <Expander.Header>
        <TextBlock Text="Settings">
            <TextBlock.LayoutTransform>
                <RotateTransform Angle="-90"/>
            </TextBlock.LayoutTransform>
        </TextBlock>
    </Expander.Header>
</Expander>

1 个答案:

答案 0 :(得分:0)

最后,我已将Collapsed和Expanded事件添加到扩展器:

<Expander Grid.Row="0" 
          Grid.RowSpan="2"
          Grid.Column="1"  
          Width="25"
          Collapsed="Expander_Collapsed"
          Expanded="Expander_Expanded"            
          ExpandDirection="Left"
          IsExpanded="False"
          HorizontalAlignment="Right">
    <Expander.Header>
        <TextBlock Text="Settings">
            <TextBlock.LayoutTransform>
                <RotateTransform Angle="-90"/>
            </TextBlock.LayoutTransform>
        </TextBlock>
    </Expander.Header>
</Expander>

然后从后面的代码中查看:

private void Expander_Collapsed(object sender, RoutedEventArgs e)
{
    ColumnDefinition c = this.Grid.ColumnDefinitions[1];
    c.Width = new GridLength(0, GridUnitType.Auto);
    this.Grid.ColumnDefinitions.RemoveAt(1);
    this.Grid.ColumnDefinitions.Insert(1, c);
}

private void Expander_Expanded(object sender, RoutedEventArgs e)
{
    ColumnDefinition c = this.Grid.ColumnDefinitions[1];
    c.Width = new GridLength(0.47, GridUnitType.Star);
    this.Grid.ColumnDefinitions.RemoveAt(1);
    this.Grid.ColumnDefinitions.Insert(1, c);
}

我认为这不会破坏MVVM模式,因为这是视图任务。