Sco*_*ttG 2 wpf controltemplate templatebinding
在我的 WPF ListBox 中,我有一个带有 ListBoxItem 的 ControlTemplate 的样式。在那个 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>
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
如果我理解正确,您可能可以执行以下类似操作,只需更改 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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18082 次 |
最近记录: |