在我的WPF列表框,我有一个ListBoxItem的一个控件模板的样式。内部的控件模板我有一个标签来定义。基于一些细节,我需要改变标签的字体大小。所以从我的代码隐藏,我需要确定的字体应该是什么,然后我需要来设置。

下面是我用的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>

我怎样才能做到这一点?

有帮助吗?

解决方案

如果我理解正确的话,你也许可以做类似于下面的东西,简单地改变对ListBoxItem的本身的字号属性;它会自动在你的标签中反映出来。这个复制到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>

其他提示

您可能能够使用 ValueConverter 的字号物业..但我不是100%肯定,如果他们一个控件模板里面工作。我似乎记得有Silverlight的问题吧,但我不记得,如果它在WPF的工作。

如果您想设置字号后面的代码中,你应该从控件模板移除字号,然后在后台代码设置为ListBoxItem的。如果您想设置相同的大小为所有ListBoxItems只是在代码中设置隐藏列表框的字号。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top