我想用TextTrimming在ViewboxPanel控件中有一個TextBlock,但爲了這樣做,TextBlock必須有一個寬度。但是在ViewboxPanel中,寬度與顯示的寬度沒有任何關係?WPF TextBlock在Viewbox中溢出
<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的用途是什麼?你可以用一些面板替換它,或者直接在網格下添加文本塊? – ShayD
目的是控制文本的大小。在我的最終應用中,viewbox通過邊距進行控制,並按百分比進行設置。 –
ViewBox當然不是你在這裏需要的。考慮使用一些Panel或Border來代替。 – ShayD