WPF如何使可扩展区域移动内容

6

我的展开菜单

你好,我希望在我的WinForms应用程序中将WPF控件添加到ElementHost中。我的控件的行为如图所示。我希望扩展任何展开器都会使我的树形控件调整为较小的大小。并且在开始时,我希望我的展开器处于折叠状态。

我尝试过像这样的代码:

<UserControl x:Class="LeftPane"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" >
<Grid VerticalAlignment="Stretch" Margin="3,3,3,3">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>            
    </Grid.RowDefinitions>
    <TreeView Grid.Row="0" Name="treeView1" VerticalAlignment="Stretch" >

    </TreeView>
    <StackPanel  Grid.Row="1" Name="StackPanel1" VerticalAlignment="Bottom">
        <ListBox SelectedIndex="1">
            <ListBoxItem VerticalAlignment="Stretch">
                <Expander Grid.Row="1" ExpandDirection="Down" Header="expander1" VerticalAlignment="Stretch" Name="expander1" IsExpanded="False">
                    <ListBox>
                        <ListBoxItem Content="Unit 1"/>
                        <ListBoxItem Content="Unit 2"/>
                    </ListBox>
                </Expander>
            </ListBoxItem>
            <ListBoxItem VerticalAlignment="Stretch">
                <Expander Grid.Row="2" ExpandDirection="Down" Header="expander2" VerticalAlignment="Stretch" Name="expander2" IsExpanded="False">
                    <ListBox>
                        <ListBoxItem Content="Unit 1"/>
                        <ListBoxItem Content="Unit 2"/>
                    </ListBox>
                </Expander>
            </ListBoxItem>
        </ListBox>
    </StackPanel>
</Grid>

and:

public void AddControl(ElementHost host)
    {
        this.parentHost = host;
        host.Child = this;
        this.Height = host.Size.Height;
        treeView1.MaxHeight = this.Height - 60;

    }

但是它不能正常工作。另外,我希望这个控件在我调整winForms窗口大小时也能调整大小。
有人可以帮助我如何设置对齐方式等吗?
2个回答

6

这是我实现此目标的方法...首先,我会将 Grid 更改为以下内容:

<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
</Grid.RowDefinitions>

这将为两个“Expander”控件提供所需的所有空间,而“TreeView”控件则使用其余空间:
<TreeView Grid.Row="0" Name="treeView1" />
<Expander Grid.Row="1" Header="expander1" Name="expander1" IsExpanded="False">
    <ListBox>
        <ListBoxItem Content="Unit 1"/>
        <ListBoxItem Content="Unit 2"/>
    </ListBox>
 </Expander>
<Expander Grid.Row="2" Header="expander2" Name="expander2" IsExpanded="False">
    <ListBox>
        <ListBoxItem Content="Unit 1"/>
        <ListBoxItem Content="Unit 2"/>
    </ListBox>
 </Expander>

1
它使一个扩展器出现在另一个扩展器上。而且更重要的是,当我调整父级 WinForms 窗口大小时,该控件不会缩放。 - santBart
@santBart 假设 Sheridan 在第二个可展开项上意味着 Grid.Row="2"。 - Alex

0
为了使我的控件可调整大小,我将ElementHost属性Dock切换为Fill。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接