繁体   English   中英

WPF TextBlock在Viewbox中溢出

[英]WPF TextBlock overflow in Viewbox

我想在带有TextTrimming的ViewboxPanel控件中有一个TextBlock,但是为​​了做到这一点,TextBlock必须具有宽度。 但是,当在ViewboxPanel中显示时,宽度与显示的宽度无关吗?

<Window x:Class="SizeTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:converters="clr-namespace:SizeTest.Utils.Converters;assembly=SizeTest.Utils"
        xmlns:controls="clr-namespace:SizeTest.Utils.Controls;assembly=SizeTest.Utils"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <converters:MarginConverter x:Key="MarginConverter"/>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <controls:ViewboxPanel HorizontalAlignment="Left">
            <controls:ViewboxPanel.Margin>
                <MultiBinding Converter="{StaticResource MarginConverter}" ConverterParameter="0;40;0;40">
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}" Path="ActualHeight" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}" Path="ActualWidth" />
                </MultiBinding>
            </controls:ViewboxPanel.Margin>
            <TextBlock Text="fgghgfhdfgfgsdfdsfdsfdsfsdf" Width="130" TextTrimming="CharacterEllipsis" />
        </controls:ViewboxPanel>

    </Grid>
</Window>

控制:

public class ViewboxPanel : Panel
        {
            private double scale;

            protected override Size MeasureOverride(Size availableSize)
            {
                double height = 0;
                Size unlimitedSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
                foreach (UIElement child in Children)
                {
                    child.Measure(unlimitedSize);
                    height += child.DesiredSize.Height;
                }
                scale = availableSize.Height / height;

                return availableSize;
            }

            protected override Size ArrangeOverride(Size finalSize)
            {
                Transform scaleTransform = new ScaleTransform(scale, scale);
                double height = 0;
                foreach (UIElement child in Children)
                {
                    child.RenderTransform = scaleTransform;
                    child.Arrange(new Rect(new Point(0, scale * height), new Size(finalSize.Width / scale, child.DesiredSize.Height)));
                    height += child.DesiredSize.Height;
                }

                return finalSize;
            }
        }

ViewboxPanel由边距和百分比控制。 但是,为什么TextBlock的130的宽度与ViewboxPanel的525(通过窗口宽度)的宽度匹配?

我希望能够调整窗口的大小,并且应该遵循文本块的宽度,以便通过修剪显示/隐藏文本。 因此,文本块的宽度应绑定到其内部网格的宽度,如下所示:

Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Path=ActualWidth}"

但我看不出为什么525不正确!

您似乎误解了ViewBox控件的用途。 从MSDN上的链接页面:

定义一个内容装饰器,该装饰器可以拉伸和缩放单个孩子来填充可用空间

为了获得更改字符省略号开始的点的预期效果,您需要更改TextBlockWidth ,而不是ViewBox

在您的情况下,视图框将尝试缩放其内部的子级,因此,如果将文本块的宽度设置为83,则视图框将根据父容器的实际宽度和高度来控制控件缩放比例。 它永远不会截断您的文本块,因为它将放大和缩小文本块以保持文本块的比例,因此,如果窗口没有足够的空间来显示文本块的内容,则视图框会将其缩小,反之亦然。

我认为,如果要使用视图框修剪文本块的内容,则应尝试将texblock的宽度绑定到窗口的宽度

<Window x:Class="SizeTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Name="WinName">
<Grid>
    <Viewbox Height="100" HorizontalAlignment="Left">
        <TextBlock Text="fgghgfhdfgfgdsfdsf" Width="{Binding Path=ActualWidth, ElementName=WinName}" TextTrimming="CharacterEllipsis" />
    </Viewbox>
</Grid>

但在这种情况下,视图框不会对您的文本块进行任何更改: 它不会缩放

如果可以满足要求,则可以尝试设置Stretch="UniformToFill"但是在这种情况下,它不会修剪您的文本。

但是我不认为这不是最好的方法,您一定误解了Viewbox的目标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM