如何停止WPF控件(使用扩展器)高度扩展到父对象';s在ItemControl中的高度

本文关键字:高度 对象 ItemControl 扩展到 控件 WPF 何停止 扩展器 | 更新日期: 2023-09-27 18:25:09

我想在ItemControl中查看UserControls主机,使用WrapPanel作为ItemsPanelTemplate,例如,I have

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal">
        <TextBlock Margin="5">No to load</TextBlock>
        <TextBox Name="NumBox" Width="80" Margin="5">1000</TextBox>
        <Button Width="80" Height="25" Click="LoadClick">Load</Button>
        <Button Click="ClearClick" Margin="5">Clear</Button>
    </StackPanel>
    <ScrollViewer  Grid.Row="1"   Margin="3" VerticalScrollBarVisibility="Auto" >
        <ItemsControl  ItemsSource="{Binding Data}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <local:TestControl2></local:TestControl2>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Grid>

TestControl2的定义如下。。

<UserControl x:Class="WpfWrapPanelTest.TestControl2"
         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" Height="Auto" Width="120" Margin="10">
<Border Background="Bisque" CornerRadius="10" Height="Auto">
    <StackPanel>
        <Expander Header="Options 1" ExpandDirection="Down">
            <StackPanel>
                <TextBlock>Item1</TextBlock>
                <TextBlock>Item2</TextBlock>
            </StackPanel>
        </Expander>
        <Expander Header="Options 2" ExpandDirection="Down">
            <StackPanel>
                <TextBlock>Item1</TextBlock>
                <TextBlock>Item2</TextBlock>
                <TextBlock>Item3</TextBlock>
                <TextBlock>Item4</TextBlock>
            </StackPanel>
        </Expander>
    </StackPanel>
 </Border>
</UserControl>

这里的问题是当我扩展其中一个扩展器时。。以及控件扩展的高度增长(我想要),同一行中的所有其他控件的高度也增长。

有没有办法阻止同一行中所有其他控件的高度增长?

提前感谢您的帮助。

如何停止WPF控件(使用扩展器)高度扩展到父对象';s在ItemControl中的高度

这里的问题是FrameworkElementVerticalAlignment属性默认为Stretch。您需要在树中的某个点上通过指定其他垂直对齐方式来打断它。我的建议是将您的DataTemplate更改为:

<local:TestControl2 VerticalAlignment="Top"></local:TestControl2>