簡體   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