2009-11-10 68 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