2009-11-10 69 views
8

我有一個WPF應用程序,其中用戶界面應該縮放,以便它應該變大,如果窗戶變大。在其中一個對話框中,我需要向用戶提供一個項目列表,用戶應該點擊其中的一個。該列表將包含從1到大約15-20項。我希望每個單獨項目的字體大小與列表中其他項目的字體大小一樣大,但同時我希望字體大小在窗口變大時增加。WPF字體縮放

目前,我的測試代碼如下所示。

<Window x:Class="WpfApplication4.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:controls="clr-namespace:WpfApplication4" 
    Title="Window1" Height="480" Width="640"> 


    <ScrollViewer> 

     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="30*" MinHeight="30"/> 
       <RowDefinition Height="30*" MinHeight="30"/> 
       <RowDefinition Height="30*" MinHeight="30"/> 
      </Grid.RowDefinitions> 

      <Button Grid.Row="0" MaxHeight="100"><Viewbox><TextBlock>T</TextBlock></Viewbox></Button> 
      <Button Grid.Row="1" MaxHeight="100"><Viewbox><TextBlock>Test</TextBlock></Viewbox></Button> 
      <Button Grid.Row="2" MaxHeight="100"><Viewbox><TextBlock>Test Longer String</TextBlock></Viewbox></Button> 

     </Grid> 

    </ScrollViewer> 

</Window> 

如果在應用程序啓動和窗口由寬,一切正常。如果窗口寬度減小,文本Test Longer String的字體大小會變小,但TTest的字體大小保持不變。我明白爲什麼會發生這種情況 - 視框會將內容縮放到最大尺寸。我想知道的是我應該用什麼方法來解決這個問題。

我不想爲控件指定特定的字體大小,因爲有些人會在640x480等低分辨率屏幕上運行此功能,而其他人則會使用較大的寬屏幕。

編輯:

我試圖修改我的代碼如下:

<ScrollViewer> 
    <Viewbox> 
     <ItemsControl> 
      <Button>Test 2</Button> 
      <Button>Test 3</Button> 
      <Button>Test 4 afdsfdsa fds afdsaf</Button> 
      <Button>Test 5</Button> 
      <Button>Test 5</Button> 
      <Button>Test 5</Button> 
      <Button>Test 5</Button> 
      <Button>Test 5</Button> 
     </ItemsControl> 
    </Viewbox> 
</ScrollViewer> 

但與按鈕邊框的大小在大屏幕上增加爲好,這樣,按鈕邊框變成釐米寬。

回答

0

試試這個:渲染文本的項目,如你會做任何其他的時間:包含在「ItemsControl中」

  • TextBlock S'
  • ListBox

將整個shebang放入ViewBox並將其設置爲適合您需要的比例。尋找水平,垂直或組合縮放屬性。

+0

我已更新我的帖子。請參閱「編輯」。簡而言之,如果我使用TextBlock而不是Button,則會鬆開「Button」-user界面(點擊效果等)。如果我使用按鈕並將其放置在Viewbox內部,則整個按鈕(包括其邊框)將被調整大小。僅僅因爲程序最大化,我不需要1cm的按鈕邊框。 – Martin 2009-11-10 12:28:37

0

此解決方案可以幫助:

<ScrollViewer> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="30*" MinHeight="30"/> 
      <RowDefinition Height="30*" MinHeight="30"/> 
      <RowDefinition Height="30*" MinHeight="30"/> 
     </Grid.RowDefinitions> 

     <Button Grid.Row="0" MaxHeight="100"> 
      <Viewbox> 
       <TextBlock Width="{Binding ElementName=tbLonger,Path=ActualWidth}">T</TextBlock> 
      </Viewbox> 
     </Button> 
     <Button Grid.Row="1" MaxHeight="100"> 
      <Viewbox> 
       <TextBlock Width="{Binding ElementName=tbLonger,Path=ActualWidth}">Test</TextBlock> 
      </Viewbox> 
     </Button> 
     <Button Grid.Row="2" MaxHeight="100"> 
      <Viewbox> 
       <TextBlock Name="tbLonger">Test Longer String</TextBlock> 
      </Viewbox> 
     </Button> 
    </Grid> 
</ScrollViewer> 

的關鍵是將所有的TextBlocks相同的寬度。在這種情況下,它們都通過綁定遵循最長的文本塊。

+0

我相信OP的代碼在按鈕寬度方面沒有問題,當窗口變小時它們的長度都是相同的,但是他希望字體大小相同(或者我暗示的那樣)。所以,綁定應該是最長文本的字體大小。 – 2016-11-17 13:13:15

-1

最簡單的方法可能是構造一個裝飾器來完成這項工作。你可以稱之爲「VerticalStretchDecorator」或類似的東西。

下面是它如何被使用:

<UniformGrid Rows="3" MaxHeight="300"> 
    <Button> 
    <my:VerticalStretchDecorator> 
     <TextBlock>T</TextBlock> 
    </my:VerticalStretchDecorator> 
    </Button> 
    <Button> 
    <my:VerticalStretchDecorator> 
     <TextBlock>Test</TextBlock> 
    </my:VerticalStretchDecorator> 
    </Button> 
    <Button> 
    <my:VerticalStretchDecorator> 
     <TextBlock>Test Longer String</TextBlock> 
    </my:VerticalStretchDecorator> 
    </Button> 
</UniformGrid> 

我用UniformGrid代替Grid,但它的工作方式相同與Grid

這將實現這樣的事:

public class VerticalStretchDecorator : Decorator 
{ 
    protected override Size MeasureOverride(Size constraint) 
    { 
    var desired = base.Measure(constraint); 
    if(desired.Height > constraint.Height || desired.Height==0) 
     LayoutTransform = null; 
    else 
    { 
     var scale = constraint.Height/desired.Height; 
     LayoutTransform = new ScaleTransform(scale, scale); // Stretch in both directions 
    } 
    } 
} 
+0

此代碼不會編譯任何內容。 Measure()返回void,並且MeasureOverride()中沒有返回值 – Bob 2011-09-21 17:36:58