用户可调整大小的 WPF 控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11766049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:28:39  来源:igfitidea点击:

User-resizable WPF Controls?

wpfxaml

提问by user316117

In XAML, how do I make it possible to resize controls or portions of a display the way the different panels like the Toolbox, Solution Explorer or Error List in Visual Studio can be grabbed and resized?

在 XAML 中,如何才能像在 Visual Studio 中抓取和调整不同面板(如工具箱、解决方案资源管理器或错误列表)一样调整控件或显示部分的大小?

In this made-up example . . .

在这个虚构的例子中。. .

<Window x:Class="UI_Experiments_1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel x:Name="Tab3DockPanel" Background="#FFA0FFA0" LastChildFill="True">
        <ScrollViewer DockPanel.Dock="Left" Background="Lavender">
            <TextBlock Height="60" TextWrapping="Wrap" Background="#FFFFDDDD" Width="140">
                  ScrollViewer - DockPanel.Dock="Left"
            </TextBlock>
        </ScrollViewer> 
        <TextBlock DockPanel.Dock="Top" HorizontalAlignment="Center" 
               TextWrapping="Wrap" Background="LemonChiffon">
               DockPanel.Dock="Top" HorizontalAlignment="Center"
        </TextBlock>
        <ListBox DockPanel.Dock="Right" Background="#FFCCEEFF">
               ListBox DockPanel.Dock="Bottom" 
        </ListBox>
    </DockPanel>
</Window>

. . . I have a DockPanel with a ScrollView docked on the left, a ListBox docked on the bottom, and a TextBlock on the top. Is there a way to attach resizing handles to these to achieve the same effect, or is there some other control that these can be embedded in? As I said, the above is just a made-up example to experiment with - I don't care if I use those exact controls.

. . . 我有一个 DockPanel,左边有一个 ScrollView,底部有一个 ListBox,顶部有一个 TextBlock。有没有办法将调整大小的手柄附加到这些手柄上以达到相同的效果,或者是否可以嵌入其他一些控件?正如我所说,以上只是一个虚构的实验示例 - 我不在乎我是否使用那些精确的控件。

I did find an example of adding resizing handles using an Adorner on MSDN but it involved over 170 lines of C# code so before adopting that I wanted to be sure there was no intrinsic way to achieve this in XAML.

我确实在 MSDN 上找到了一个使用 Adorner 添加调整大小句柄的示例,但它涉及 170 多行 C# 代码,因此在采用之前我想确保在 XAML 中没有内在的方法来实现这一点。

Thanks in advance.

提前致谢。

采纳答案by Vlad Bezden

You might go with GridSplitterWhat you will need to do is to do your layout with Grid and then use GridSplitter to resize columns or rows.

您可能会使用GridSplitter您需要做的是使用 Grid 进行布局,然后使用 GridSplitter 调整列或行的大小。

Here is an example on How to: Create User-Resizable Applications with GridSplitter

以下是关于如何:使用 GridSplitter 创建用户可调整大小的应用程序的示例

回答by paparazzo

Not the exact controls you asked but a sample. Need a splitter and what is on either side * and the contained control stretch.

不是您要求的确切控件,而是示例。需要一个分离器,两边是什么 * 以及包含的控制拉伸。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="*" />     
    </Grid.ColumnDefinitions>
    <TextBox Grid.Row="0" Grid.Column="0" 
             HorizontalAlignment="Stretch" 
             VerticalAlignment="Stretch" 
             Text="TexBox" />      
    <GridSplitter Grid.Row="0" Grid.Column="1" Margin="2,0,2,0"
                  Width="3" Background="Purple" 
                  VerticalAlignment="Stretch" 
                  HorizontalAlignment="Center" />
    <ListView Grid.Row="0" Grid.Column="2" Background="Aqua" 
              HorizontalAlignment="Stretch" 
              VerticalAlignment="Stretch"/>
</Grid>