[英]C# Foreground / Background TemplateBinding not applied on initial setter but within trigger
I'm trying to set the foreground of a TextBlock within a custom control within an initial setter, but the color remains default.我正在尝试在初始设置器中的自定义控件中设置 TextBlock 的前景,但颜色保持默认。 The strange thing is doing it within a Trigger it works just fine.
奇怪的是在触发器中做它工作得很好。
I also added TemplateBinding to the TextBlock.Foreground property within the custom control but it doesn't make a difference.我还在自定义控件中的 TextBlock.Foreground 属性中添加了 TemplateBinding,但这并没有什么不同。
MainWindow.xaml:主窗口.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: 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>
That's how it looks like after starting the app:这是启动应用程序后的样子:
Try to move the trigger to the ControlTemplate
and set the Foreground
property of the TextBlock
directly:尝试将触发器移动到
ControlTemplate
并直接设置TextBlock
的Foreground
属性:
<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 x:Name="tb"
Grid.Column="1"
Margin="10,0,0,0"
VerticalAlignment="Center"
Foreground="White"
Text="{TemplateBinding Content}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="tb" Property="Foreground" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
If this doesn't work, you are probably setting the Foreground
property somewhere else in your code.如果这不起作用,您可能在代码中的其他位置设置了
Foreground
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.