我尝试在初始设置器中设置自定义控件中TextBlock的前景,但颜色仍然是默认的。奇怪的是在Trigger中这样做,它工作得很好。
我还将TemplateBinding添加到自定义控件中的TextBlock.Foreground属性,但这并没有什么不同。
MainWindow.xaml:
<controls:NavigationDrawer
Grid.Column="0"
Background="#212121"
IsOpen="{Binding IsChecked, ElementName=cbToggleNavigationDrawer}"
OpenCloseDuration="0:0:0.25">
<controls:NavigationDrawer.Resources>
<Style TargetType="controls:NavigationDrawerItem">
<!-- does not work -->
<Setter Property="Foreground" Value="White" />
<Setter Property="ImageSize" Value="50" />
<Setter Property="Margin" Value="0,20,0,0" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="FontWeight" Value="SemiBold" />
<!-- works perfectly fine -->
<Setter Property="Foreground" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</controls:NavigationDrawer.Resources>
<controls:NavigationDrawer.Content>
<StackPanel Margin="10,10">
<controls:NavigationDrawerItem Content="Home" ImageSource="/Resources/home-white.png" />
<controls:NavigationDrawerItem Content="Launchers" ImageSource="/Resources/launcher-white.png" />
<controls:NavigationDrawerItem Content="Games" ImageSource="/Resources/game-white.png" />
<controls:NavigationDrawerItem Content="Contribute" ImageSource="/Resources/contribute-white.png" />
<controls:NavigationDrawerItem Content="About" ImageSource="/Resources/about-white.png" />
</StackPanel>
</controls:NavigationDrawer.Content>
</controls:NavigationDrawer>
NavigationDrawerItemStyle.xaml:
<Style TargetType="{x:Type controls:NavigationDrawerItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:NavigationDrawerItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Viewbox
Grid.Column="0"
Width="{TemplateBinding ImageSize}"
Height="{TemplateBinding ImageSize}">
<Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}" />
</Viewbox>
<TextBlock
Grid.Column="1"
Margin="10,0,0,0"
VerticalAlignment="Center"
Text="{TemplateBinding Content}" />
<!--
Does not make a difference to add
Foreground="{TemplateBinding Foreground}"
to TextBlock
-->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这是启动应用程序后的外观:
1条答案
按热度按时间ejk8hzay1#
尝试将触发器移动到
ControlTemplate
,并直接设置TextBlock
的Foreground
属性:如果这样做不起作用,则可能是在代码的其他位置设置了
Foreground
属性。