2014-12-23 40 views
0

我想用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會是不正確的!?

+0

ViewBox的用途是什麼?你可以用一些面板替換它,或者直接在網格下添加文本塊? – ShayD

+0

目的是控制文本的大小。在我的最終應用中,viewbox通過邊距進行控制,並按百分比進行設置。 –

+0

ViewBox當然不是你在這裏需要的。考慮使用一些Panel或Border來代替。 – ShayD

回答

1

您似乎誤解了ViewBox control的用途。從MSDN上的鏈接頁面:

定義內容裝飾,可以拉伸和縮放一個孩子填補可用空間

爲了得到改變點什麼性格省略號開始的你想要的效果,你需要改變TextBlockWidth,而不是ViewBox

+0

爲了控制文本的大小,我並沒有改變視框的寬度,只是它的高度。 –

+0

*您需要更改TextBlock的寬度* – Sheridan

+0

是的..那不是問題。問題是,爲什麼寬度中的83像素顯示爲窗口/網格的寬度的100%? –

0

在你的情況下,viewbox試圖縮放它裏面的子元素,所以如果你設置文本塊的寬度爲83,那麼viewbox會保留控件的比例,以便根據實際的寬度和高度父容器。它永遠不會截斷你的文本塊,因爲它會縮放文本塊以保持文本塊的比例,所以如果窗口沒有足夠的空間來顯示文本塊的內容,視圖框會縮小它,反之亦然。

我認爲,如果你想使用視框微調的文本塊的內容,你應該嘗試的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",但在這種情況下,它不會裁剪文字。

但我不認爲這不是最好的方法,你一定誤解了視框的目標。