【问题标题】:How to stop WPF controls (using expanders) heights expanding to parent's height in ItemControl如何停止 WPF 控件(使用扩展器)高度扩展到 ItemsControl 中的父高度
【发布时间】:2015-11-30 02:51:02
【问题描述】:

我想在 ItemControl 中查看 UserControls hostest,使用 WrapPanel 作为 ItemsPanelTemplate,例如,我有

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

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

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

提前感谢您的帮助。

【问题讨论】:

  • 无法重现你的问题,尝试用图解释。
  • @AnjumSKhan 我重现,也许你需要在代码隐藏的“数据”中添加一些项目。第一个答案解决了它。
  • @LeiYang 我看不到我的控件在高度中增长。 share.pho.to/9u3cc
  • @LeiYang 对不起,我明白了。

标签: c# .net wpf windows


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2018-10-06
    • 2011-06-15
    • 2011-05-30
    • 2018-08-28
    • 2018-09-30
    相关资源
    最近更新 更多