C# wpf拆分器切换方向

C# wpf拆分器切换方向,c#,wpf,C#,Wpf,我知道如何在winforms中实现这一点,但在wpf中似乎并不容易。在wpf中使用拆分器时,如何切换方向(垂直/水平)?我试图创建一个简单的例子,只需一个按钮切换切换 在winforms中,我会在切换按钮中添加类似的内容 private void button1_Click(object sender, EventArgs e) { splitContainer1.Orientation = splitContainer1.Orientation == O

我知道如何在winforms中实现这一点,但在wpf中似乎并不容易。在wpf中使用拆分器时,如何切换方向(垂直/水平)?我试图创建一个简单的例子,只需一个按钮切换切换

在winforms中,我会在切换按钮中添加类似的内容

private void button1_Click(object sender, EventArgs e)
        {
            splitContainer1.Orientation = splitContainer1.Orientation == Orientation.Vertical ? Orientation.Horizontal : Orientation.Vertical; 
        }
在wpf中,我得到了这一点,因为拆分器没有方向属性

<Window x:Class="WpfTutorialSamples.Panels.GridSplitterHorizontalSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="GridSplitterHorizontalSample" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="5" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBlock FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Top</TextBlock>
        <GridSplitter Grid.Row="1" Height="5" HorizontalAlignment="Stretch" />
        <TextBlock Grid.Row="2" FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Bottom</TextBlock>
    </Grid>
</Window>

顶部
底部

Edit2/edit3

我对这个解决方案不是很满意,因为您可以有效地定义两个视图,而不是重复使用一个定义,只是更改一个属性,但它可能有助于您理解可能的解决方案

我更新了XAML,以显示如何将内容区域与网格布局分离,以及如何通过ContentPresenter传播值

在XAML中,将所需视图定义为资源,并使用
ContentControl
选择模板:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded">
    <Window.Resources>
        <DataTemplate x:Key="content1">
            <Label Content="{Binding}" Background="Yellow"/>
        </DataTemplate>
        <DataTemplate x:Key="content2">
            <Label Content="Content Part 2"/>
        </DataTemplate>
        <ControlTemplate x:Key="gridHorizontal">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="5"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <ContentPresenter Grid.Row="0" Grid.Column="0" Content="{Binding}" ContentTemplate="{StaticResource content1}"/>
                <ContentPresenter Grid.Row="0" Grid.Column="2" ContentTemplate="{StaticResource content2}"/>
                <GridSplitter Grid.Row="0" Grid.Column="1" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            </Grid>
        </ControlTemplate>

        <ControlTemplate x:Key="gridVertical">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="5"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <ContentPresenter Grid.Row="0" Grid.Column="0" Content="{Binding}" />
                <ContentPresenter Grid.Row="2" Grid.Column="0" ContentTemplate="{StaticResource content2}"/>
                <GridSplitter Grid.Row="1" Grid.Column="0" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            </Grid>
        </ControlTemplate>
    </Window.Resources>
    <DockPanel>
        <Button DockPanel.Dock="Bottom" HorizontalAlignment="Left" Content="Transform" Click="Button_Click"/>
        <ContentControl x:Name="contentControl1" DataContext="Hello World" Template="{StaticResource ResourceKey=gridVertical}"/>
    </DockPanel>
</Window>
编辑

查看
ResizeDirection
属性

原创

我没有确切的来源,但我的实验表明,拆分器总是沿着其长边界的方向工作

因此,宽度=10,高度=9的拆分器将用作上/下拆分器,而宽度=9,高度=10的拆分器将用作左/右拆分器

要使用的示例:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="40"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Label Grid.Row="0" Grid.Column="0" Content="Upper Left"/>
    <Label Grid.Row="0" Grid.Column="2" Content="Upper Right"/>
    <Label Grid.Row="2" Grid.Column="0" Content="Lower Left"/>
    <Label Grid.Row="2" Grid.Column="2" Content="Lower Right"/>
    <GridSplitter Grid.Row="1" Grid.Column="0" Background="Yellow" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    <GridSplitter Grid.Row="0" Grid.Column="1" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>

开始时,红色拆分器是无用的,因为它试图提供它无法实现的功能,但一旦黄色拆分器向下拉一点,红色拆分器就会转换为左/右拆分器

希望有帮助。


<Window x:Class="WpfTutorialSamples.Panels.GridSplitterHorizontalSample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GridSplitterHorizontalSample" Height="300" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="5" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBlock FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Top</TextBlock>
    <GridSplitter Grid.Row="1" Height="5" HorizontalAlignment="Stretch" ResizeDirection="Vertical" />
    <TextBlock Grid.Row="2" FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Bottom</TextBlock>
</Grid>
顶部 底部

你可以试试这样的
请注意GridSplitter对象中的ResizeDirection属性

您使用的.net版本。上面说财产无效。我更新了帖子,以便更清楚地了解我想做什么。下面的两种尝试似乎都不起作用……这是一个很酷的想法,而且似乎效果很好,我知道你提到这不是一个想法,但实际上是什么,我认为它很好。
<Window x:Class="WpfTutorialSamples.Panels.GridSplitterHorizontalSample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GridSplitterHorizontalSample" Height="300" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="5" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBlock FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Top</TextBlock>
    <GridSplitter Grid.Row="1" Height="5" HorizontalAlignment="Stretch" ResizeDirection="Vertical" />
    <TextBlock Grid.Row="2" FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Bottom</TextBlock>
</Grid>