Dynamic horizontal/vertical split change with AvalonDock

送分小仙女□ 提交于 2019-12-06 15:02:26

I have not looked into AvalonDock, but if you just need a changeable GridSplitter, I would suggest the following:

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

Where BoolConverter is an IValueConverter. And the code behind:

private bool _layoutHorizontal = true;
public bool LayoutHorizontal
{
    get { return _layoutHorizontal; }
    set
    {
        _layoutHorizontal = value;
        NotifyPropertyChanged();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!