2017-01-20 62 views
1

我有我的界面(使用WPF),我想分裂兩個領域。還有一個按鈕可以在水平和垂直分割之間切換。我正在使用AvalonDock。在運行之前更改代碼中的方向參數時,它都可以工作。動態水平/垂直拆分與AvalonDock變化

<ad:DockingManager Grid.Row="1"> 
     <ad:LayoutRoot> 
      <ad:LayoutPanel x:Name="LayoutPanel1" Orientation="Vertical" IsMaximized="True"> 
       <ad:LayoutDocumentPane x:Name="DocPane1" ShowHeader="True"> 
        <ad:LayoutDocument Title="Spectrogram" CanClose="False" CanFloat="False"> 
         <wpf:CartesianChart Series="{Binding MySeries}" Zoom="X"/> 
        </ad:LayoutDocument> 
       </ad:LayoutDocumentPane> 
       <ad:LayoutDocumentPane x:Name="DocPane2" ShowHeader="True"> 
        <ad:LayoutDocument Title="Table" CanClose="False" CanFloat="False"> 
         <TextBox Name="textbox1" /> 
        </ad:LayoutDocument> 
       </ad:LayoutDocumentPane> 
      </ad:LayoutPanel> 
     </ad:LayoutRoot> 
    </ad:DockingManager> 

但它不會改變按鈕點擊這裏。沒有任何反應,但是當我嘗試拖動保持在原位的分離器時,程序崩潰。

private void OnChangeView(object sender, RoutedEventArgs e) 
    { 
     if (LayoutPanel1.Orientation == Orientation.Vertical) { 
      LayoutPanel1.Orientation = Orientation.Horizontal; 
     } else { 
      LayoutPanel1.Orientation = Orientation.Vertical; 
     } 
    } 

我調試了它,屬性本身發生了變化。不知道問題是什麼... 或者你也許知道一個更好的方法來實現這一點,但我也可能以後需要AvalonDock。

回答

0

我還沒有研究AvalonDock,但如果你只需要一個多變GridSplitter,我建議如下:

<ContentControl> 
<ContentControl.Resources> 
    <BoolConverter x:Key="BoolToLayoutConverter" TrueValue="templateHorizontal" FalseValue="templateVertical"/> 
    <BoolConverter x:Key="BoolToLayoutCharacterConverter" TrueValue="—" FalseValue="|"/> 
    <DataTemplate x:Key="mainTable"> 
     <StackPanel> 
      <Label Content="MainTable goes here"/> 
      <ToggleButton Content="{Binding LayoutHorizontal, Converter={StaticResource BoolToLayoutCharacterConverter}}" 
        IsChecked="{Binding LayoutHorizontal}"/> 
     </StackPanel> 
    </DataTemplate> 
    <DataTemplate x:Key="childTables"> 
     <Label Content="ChildTables go here"/> 
    </DataTemplate> 
</ContentControl.Resources> 
<ContentControl.Style> 
    <Style TargetType="ContentControl"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding LayoutHorizontal}" Value="False"> 
       <Setter Property="ContentTemplate"> 
        <Setter.Value> 
         <DataTemplate> 
          <Grid> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition/> 
            <ColumnDefinition Width="10"/> 
            <ColumnDefinition/> 
           </Grid.ColumnDefinitions> 
           <ContentPresenter Grid.Column="0" ContentTemplate="{StaticResource mainTable}"/> 
           <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/> 
           <ContentPresenter Grid.Column="2" ContentTemplate="{StaticResource childTables}"/> 
          </Grid> 
         </DataTemplate> 
        </Setter.Value> 
       </Setter> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding LayoutHorizontal}" Value="True"> 
       <Setter Property="ContentTemplate"> 
        <Setter.Value> 
         <DataTemplate> 
          <Grid> 
           <Grid.RowDefinitions> 
            <RowDefinition/> 
            <RowDefinition Height="5"/> 
            <RowDefinition/> 
           </Grid.RowDefinitions> 
           <ContentPresenter Grid.Row="0" ContentTemplate="{StaticResource mainTable}"/> 
           <GridSplitter Grid.Row="1" Height="10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/> 
           <ContentPresenter Grid.Row="2" ContentTemplate="{StaticResource childTables}"/> 
          </Grid> 
         </DataTemplate> 
        </Setter.Value> 
       </Setter> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</ContentControl.Style> 
</ContentControl> 

凡BoolConverter是的IValueConverter。和後面的代碼:

private bool _layoutHorizontal = true; 
public bool LayoutHorizontal 
{ 
    get { return _layoutHorizontal; } 
    set 
    { 
     _layoutHorizontal = value; 
     NotifyPropertyChanged(); 
    } 
}