2017-06-19 82 views
0

我有一個向左和向右擴展的窗口。將窗口向左和向右擴展,但將主要內容保留在相同位置

簡單的例子:

<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看看。

解決方案的作品,但只解決了一半的問題。

如何做左和右?

UPDATE

  • 我不希望擴展列加以固定。他們應該只顯示所需要的擴展

回答

0

一種方法,將工作的最小空間是這樣的:

(但我用這個解決方案在某種程度上不滿意)

創建Enum存儲最後行動:

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

然後,我添加了處理程序Expand & CollapseExpanders xaml。

最後覆蓋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

+0

它看起來相當整潔我,應該是一個很好的解決方案。 PS,我喜歡你展示你的問題的動畫:P –

相關問題