2009-01-19 105 views
2

在我的WPF列表框中,我有一个带有ControlTemplate的样式的ListBoxItem样式。在ControlTemplate的内部,我定义了一个标签。根据一些细节,我需要更改标签的字体大小。所以从我的代码隐藏,我需要确定字体应该是什么,然后我需要设置它。如何在ControlTemplate中更改标签的字体大小

这是我与控件模板样式(我已经去掉了一些无关痛痒的控制)

<Style x:Key="RecordTabList" TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="Background" Value="{DynamicResource RecordIndexTabBackcolor}" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate>   
          <Label 
           x:Name="myLabel" Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="0" Grid.RowSpan="1" Margin="3,-2,0,-2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Foreground="{DynamicResource RecordIndexTabForeground}" 
           FontSize="10" Height="Auto" BorderThickness="3,0,0,0" 
           Content="{Binding Path=Name}" /> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 

我怎样才能做到这一点?

回答

4

如果我理解你正确,你可以做类似于下面的事情,并简单地改变ListBoxItem本身的FontSize属性;它会自动反映在您的标签上。将其复制到VS中,并在行动中看到它!

<Window.Resources> 
    <Style TargetType="ListBoxItem"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBoxItem"> 
        <Label Content="{TemplateBinding Content}" FontSize="{TemplateBinding FontSize}"/> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
<Grid> 
    <ListBox Margin="12"> 
     <ListBoxItem Content="Test 1" FontSize="14"/> 
     <ListBoxItem Content="Test 2" FontSize="18"/> 
     <ListBoxItem Content="Test 3" FontSize="22"/> 
    </ListBox> 
</Grid> 
0

您可能可以在FontSize属性上使用ValueConverter ..但我不是100%确定它们是否在ControlTemplate中工作..我似乎记得Silverlight有问题,但我记不起如果它在WPF中工作。

0

如果你想设置字号后面的代码中,你应该从控件模板移除字号,然后将其设置在代码隐藏的ListBoxItem中。如果要为所有ListBoxItems设置相同的大小,只需在代码隐藏中设置ListBox的FontSize即可。

相关问题