Reputation: 75
I'm trying to set the foreground of a TextBlock within a custom control within an initial setter, but the color remains default. 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.
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>
That's how it looks like after starting the app:
Upvotes: 0
Views: 150
Reputation: 169150
Try to move the trigger to the ControlTemplate
and set the Foreground
property of the TextBlock
directly:
<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.
Upvotes: 1