Я хочу иметь TextBlock в элементе управления ViewboxPanel с TextTrimming, но для этого TextBlock должен иметь ширину. Но как же ширина не имеет ничего общего с показанной шириной, когда в ViewboxPanel?Переполнение WPF TextBlock в окне просмотра
<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 контролируется краем и процент. Но почему ширина 130 TextBlock соответствует ширине ViewboxPanel, которая равна 525 (по ширине окна)?
Я хочу, чтобы иметь возможность изменять размер окна, а ширина текстового блока должна следовать, чтобы он отображал/скрывал текст, обрезая его. Таким образом, ширина TextBlock следует связать с шириной сетки ИТС, например:
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Path=ActualWidth}"
Но я не могу понять, почему 525 не будет правильно !?
Какова цель ViewBox? Можете ли вы заменить его какой-либо панелью или просто добавить Textblock прямо под Grid? – ShayD
Целью является контроль размера текста. В моем конечном приложении viewbox контролируется через его поля и устанавливается в процентах. –
ViewBox, безусловно, не то, что вам нужно здесь. Подумайте об использовании какой-либо панели или границы. – ShayD