2

我在 a 中有一个文本框,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>
4

1 回答 1

1

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

一些开发人员试图根据<TextBlock />. 这是一个好主意,但很难执行。事实证明,UI Element 的大小在渲染之前是不确定的,因此您必须先渲染它,然后为时已晚。如果您想了解一位开发人员是如何完成此计算的(考虑字体系列和字体大小和边距等的难度),请查看此处。这样的计算将允许您使用<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);
}

祝你好运。

于 2013-01-24T18:56:13.683 回答