gpt4 book ai didi

c# - 垂直调整 StackPanel 的大小以适应其内容

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:29 24 4
gpt4 key购买 nike

我在 StackPanel 中有一个文本框,TextBox 设置为 AcceptsReturn,所以当您按下 Enter/Return 键时,文本框高度会变大。

我遇到的问题是我不知道如何使周围的 StackPanel 随文本框一起改变高度。因此,当文本框更改时,StackPanel 也应更改。

我们如何做到这一点?

    <GridView x:Name="texties" Grid.Row="1" Margin="120, 0, 0, 0" ItemsSource="{Binding Something, Mode=TwoWay}" SelectionMode="Multiple">
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10" Orientation="Vertical" Width="210" >
<StackPanel.ChildrenTransitions>
<TransitionCollection>
<AddDeleteThemeTransition/>
</TransitionCollection>
</StackPanel.ChildrenTransitions>
<TextBlock Text="{Binding Name, Mode=TwoWay}" FontWeight="Bold" Style="{StaticResource ItemTextStyle}" />
<TextBox Text="{Binding Content, Mode=TwoWay}" FontSize="12" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="0, 0, 0, 0" AcceptsReturn="True" IsSpellCheckEnabled="True" />
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>

最佳答案

根据您的示例,您没有设置 GridView.ItemsPanel .默认值 GridView.ItemsPanel<WrapGrid />它具有无法更改的设置单元格大小。您可能想更新到 <VariableSizedWrapGrid />但此控件不能更改跨度值,除非在渲染期间。如果你想要的甚至是可能的,它会要求你使用 <StackPanel/>作为你的GridView.ItemsPanel .然而,<StackPanel/>不在本地包装,因此您需要找到其他人制作的包装版本、制作您自己的包装版本,或者将其放在一行或一列中。

一些开发者试图根据他们的 <TextBlock /> 的高度来改变他们模板的大小。 .这是一个好主意,但很难执行。事实证明,UI 元素的大小在渲染之前是不确定的,因此您必须先渲染它,然后就为时已晚。如果您想了解开发人员如何完成此计算(考虑字体系列、字体大小和边距等的难度),请查看 here .这样的计算将允许您使用 <VariableSizedWrapGrid /> .

在此示例中,他正在计算一个椭圆,但它是~相同的计算。

protected override Size MeasureOverride(Size availableSize)
{
// just to make the code easier to read
bool wrapping = this.TextWrapping == TextWrapping.Wrap;


Size unboundSize = wrapping ? new Size(availableSize.Width, double.PositiveInfinity) : new Size(double.PositiveInfinity, availableSize.Height);
string reducedText = this.Text;


// set the text and measure it to see if it fits without alteration
if (string.IsNullOrEmpty(reducedText)) reducedText = string.Empty;
this.textBlock.Text = reducedText;
Size textSize = base.MeasureOverride(unboundSize);


while (wrapping ? textSize.Height > availableSize.Height : textSize.Width > availableSize.Width)
{
int prevLength = reducedText.Length;

if (reducedText.Length > 0)
reducedText = this.ReduceText(reducedText);

if (reducedText.Length == prevLength)
break;

this.textBlock.Text = reducedText + "...";
textSize = base.MeasureOverride(unboundSize);
}

return base.MeasureOverride(availableSize);
}

祝你好运。

关于c# - 垂直调整 StackPanel 的大小以适应其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14466826/

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