简体   繁体   English

WPF TextBlock在Viewbox中溢出

[英]WPF TextBlock overflow in Viewbox

I want to have a TextBlock in the ViewboxPanel control with TextTrimming, but in order to do so, the TextBlock must have a width. 我想在带有TextTrimming的ViewboxPanel控件中有一个TextBlock,但是为​​了做到这一点,TextBlock必须具有宽度。 But how come the width has nothing to do with the shown width when in the ViewboxPanel? 但是,当在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>

Control: 控制:

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

The ViewboxPanel is controlled by margin and percentage. ViewboxPanel由边距和百分比控制。 But why does the width of 130 of the TextBlock match the width of the ViewboxPanel which is 525 (by the window width)? 但是,为什么TextBlock的130的宽度与ViewboxPanel的525(通过窗口宽度)的宽度匹配?

I want to be able to resize the window and the width of the textblock should follow so it shows/hides the text by trimming it. 我希望能够调整窗口的大小,并且应该遵循文本块的宽度,以便通过修剪显示/隐藏文本。 So the Width of the textblock should be bind to the width of the grid its in, like this: 因此,文本块的宽度应绑定到其内部网格的宽度,如下所示:

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

But i can't see why 525 wouldn't be correct!? 但我看不出为什么525不正确!

You seem to have misunderstood the purpose of the ViewBox control . 您似乎误解了ViewBox控件的用途。 From the linked page on MSDN: 从MSDN上的链接页面:

Defines a content decorator that can stretch and scale a single child to fill the available space . 定义一个内容装饰器,该装饰器可以拉伸和缩放单个孩子来填充可用空间

In order to get your desired effect of changing what point the character ellipsis starts, you need to change the Width of the TextBlock , not the ViewBox . 为了获得更改字符省略号开始的点的预期效果,您需要更改TextBlockWidth ,而不是ViewBox

In your situation the viewbox, tries to scale the child inside it and so, if you set the width of the textblock to 83, the viewbox mantain the proportion of the control scaling it according to the actual width and height of the parent container. 在您的情况下,视图框将尝试缩放其内部的子级,因此,如果将文本块的宽度设置为83,则视图框将根据父容器的实际宽度和高度来控制控件缩放比例。 It will never truncate your textblock because it will zoom it in and out tring to maintain the proportion of the textblock and so if the window hasn't enough space to display the content of the textblock the viewbox will zoom it out and vice versa. 它永远不会截断您的文本块,因为它将放大和缩小文本块以保持文本块的比例,因此,如果窗口没有足够的空间来显示文本块的内容,则视图框会将其缩小,反之亦然。

I think that if you want to use the viewbox trimming the content of the textblock you should try to bind the width of the texblock to the width of the window 我认为,如果要使用视图框修剪文本块的内容,则应尝试将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>

but in this case the viewbox doesn't make any change to your textblock: it won't be scaled . 但在这种情况下,视图框不会对您的文本块进行任何更改: 它不会缩放

If it can satisfy, you could try to set Stretch="UniformToFill" but in this case it won't trim your text. 如果可以满足要求,则可以尝试设置Stretch="UniformToFill"但是在这种情况下,它不会修剪您的文本。

But i don't think it is not the best approach, you must have been misunderstood the target of the viewbox. 但是我不认为这不是最好的方法,您一定误解了Viewbox的目标。

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

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