将窗口展开到LEFT&对,但保持主要内容在同一位置

时间:2017-06-19 10:04:43

标签: c# wpf resize expand frameworkelement

我有一个向左和向右扩展的窗口。

简化示例:

<Window x:Class="Application1.Windows.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="MainWindow"
    SizeToContent="Width" Height="250">   
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />                <!--Expander LEFT-->
            <ColumnDefinition Width="25"/>
            <ColumnDefinition Width="175" />                <!--Red Content -->
            <ColumnDefinition Width="25"/>
            <ColumnDefinition Width="Auto"/>                <!--Expander RIGHT-->
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="25"/>
        </Grid.RowDefinitions>      
    <Expander Grid.Column="0"
              Grid.Row="0"
              Grid.RowSpan="3"
              Margin="5"
              ExpandDirection="Left">
        <Grid Width="200">                
        </Grid>
    </Expander>
    <Grid Grid.Column="2"                  
          Grid.RowSpan="3" 
          Background="Red"/>
        <Expander Grid.Column="4"
                  Grid.Row="0"
                  Grid.RowSpan="3"
                  Margin="5"
                  ExpandDirection="Right">
        <Grid Width="200">
        </Grid>
    </Expander>
    </Grid>

在这里你可以看到问题所在:

enter image description here

我已经看过WPF - Expand Window to the Left

解决方案类型有效,但这只能解决问题的一半。

如何为左和右做?

更新

  • 我不希望修复Expander列。它们应该只显示扩展器所需的最小空间

1 个答案:

答案 0 :(得分:0)

一种可行的方法是:

(但我对此解决方案不满意)

创建Enum以将最后一个操作存储在:

public enum ExpandActions
{
    ExpandRight,
    ExpandLeft,
    CollapsRight,
    CollapseLeft
}

然后我添加了Expand&amp;的处理程序xaml中的Collapse Expanders

最后在窗口覆盖SizeChanged,如WPF - Expand Window to the Left

我们只想要这种行为,当向左扩展时 - 否则我们会杀死我们'向右扩展'的行为。

protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
    if (!sizeInfo.WidthChanged)
    {
        base.OnRenderSizeChanged(sizeInfo);
        return;
    }

    Hide();
    base.OnRenderSizeChanged(sizeInfo);

    //Last action was on left expander
    if(_lastAction == ExpandActions.CollapseLeft || _lastAction == ExpandActions.ExpandLeft)
    {
        Left -= (sizeInfo.NewSize.Width - sizeInfo.PreviousSize.Width);
    }                    
    Show();
}

关于这一点的好处是可以处理屏幕扩展。

它有效,但我想这不是最好的解决方案。

结果如下:

enter image description here