2

I have a templated treeview that is working just as i want, but i have a context menu which allows for a tier 1 node to be marked as "default".

I have a datatrigger which reacts to a property in the viewmodel, which should change the fontweight to bold just to visually show that this is the default node. But the setter will not change the fontweight on the button no matter what!

However if i change another value, like the foreground color for example, it works just fine, font size also no problem.

Why is this, can anyone explain this? Here is some code if needed:

Trigger:

<HierarchicalDataTemplate ItemsSource="{Binding Children,Mode=TwoWay,NotifyOnSourceUpdated=True}"> <StackPanel Orientation="Horizontal" VerticalAlignment="Top"> <Image x:Name="nodeImg" Source="{Binding Image}" MaxHeight="16" MaxWidth="16"/> <Button x:Name="nodeButton" Content="{Binding Name}" Command="{Binding Command}" Style="{StaticResource TreeMenuButton}"/> </StackPanel> <HierarchicalDataTemplate.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem},Path=IsExpanded}" Value="True"> <Setter TargetName="nodeImg" Property="Source" Value="{Binding ImageExpanded}"></Setter> </DataTrigger> <DataTrigger Binding="{Binding IsDefaultConnection}" Value="True"> <Setter TargetName="nodeButton" Property="FontWeight" Value="Bold"></Setter> </DataTrigger> </HierarchicalDataTemplate.Triggers> </HierarchicalDataTemplate> 

Default style on button:

<Style x:Key="TreeMenuButton" TargetType="{x:Type Button}"> <Setter Property="SnapsToDevicePixels" Value="true" /> <Setter Property="OverridesDefaultStyle" Value="true" /> <Setter Property="MinHeight" Value="23" /> <Setter Property="MinWidth" Value="75" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="Border" CornerRadius="0" BorderThickness="0" Background="Transparent" BorderBrush="Transparent"> <ContentPresenter Margin="2" HorizontalAlignment="Left" VerticalAlignment="Center" RecognizesAccessKey="True"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> 
2
  • Does the font that you are using in your button have a bold style, and if so, is it visually different at the size you are currently displaying? Commented Sep 11, 2013 at 15:35
  • Yes, the thing is that i have a different datacontext in the designer with mockup data, and i can actually see it work properly in the designer. I can even change the fontfamily/weight etc, but as soon as i run it, it just does not work Commented Sep 12, 2013 at 12:36

1 Answer 1

2

I'd be surprised if your code even built as you can't add a DataTrigger into a UIElement.Triggers collection. Try using a Style instead (this definitely works):

<Style> <Style.Triggers> <DataTrigger Binding="{Binding IsDefault}" Value="True"> <Setter Property="TextBlock.FontWeight" Value="Bold" /> </DataTrigger> </Style.Triggers> </Style> 

If by some miracle your DataTemplate does work in a UIElement.Triggers collection, then try using the class name as well as the property name:

<Setter Property="TextBlock.FontWeight" Value="Bold" /> 
Sign up to request clarification or add additional context in comments.

3 Comments

I updated my style for the button so now has your code in it, but it still wont affect the button text =(. FontSize etc still works fine when using your way though.
No i want to change the FontWeight to bold, but that specific change has no effect, i can still only change the FontSize/ForeColor. Also tried to change the template of the button, so that instead of a ContentPresenter i have a TextBlock with x:Name="textBlock" in it, and then in the style trigger refer to the TextBlock with TargetName. But still the same result, can only change fontsize/forecolor =(. Another hint is that the style i set for each button,i tried to make it default that every node has FontWeight=bold via a <setter> in the style, it wont even take that =(.
As a temporary test, add this to your TextBox: FontFamily="Arial". If the text is now bold then your original font does not support the font weight property. If it is still not bold, then I just don't know what your problem is.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.