gpt4 book ai didi

c# - 如何停止 WPF 控件(使用扩展器)在 ItemsControl 中扩展到父级的高度

转载 作者:可可西里 更新时间:2023-11-01 11:26:32 24 4
gpt4 key购买 nike

我想在 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>

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

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

在此先感谢您的帮助。

最佳答案

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

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

关于c# - 如何停止 WPF 控件(使用扩展器)在 ItemsControl 中扩展到父级的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33990764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com