Reputation: 1368
I have a custom tooltip that I'm trying to style. I have the following:
<UserControl.ToolTip>
<StackPanel>
<TextBlock Text="{Binding Path=ToolPrototype.TypeName}" MaxWidth="200" FontWeight="Bold" TextWrapping="Wrap"></TextBlock>
<TextBlock Text="{Binding Path=ToolPrototype.Description}" MaxWidth="200" TextWrapping="Wrap"></TextBlock>
<TextBlock Text="{Binding Path=ToolPrototype.Note}" MaxWidth="200" FontWeight="Regular" FontStyle="Italic" TextWrapping="Wrap"></TextBlock>
</StackPanel>
</UserControl.ToolTip>
It's pretty straight forward. The title will be bold. The description will be regular font weight, and the 'note' will be shown in italics. The issue is that not all of my ToolPrototypes have the .Note as a member (it's basically null). Some of my tools do include a string value that is assigned to the Note property, but not all. So, in cases where the note is null, I'm still getting a blank area in my tooltip (essentially it's like a blank line). What I would like to do is only include that line if the ToolPrototype actually has the note property defined. Otherwise omit it. So, what I think I'm after is trying to conditionally check if the binding exists... but maybe that's not the right way to think about it. Can anyone provide some guidance?
Update
Working off of ASh's suggestion, I'm trying to work with DataTriggers. Here's what I have now.
<UserControl.ToolTip>
<StackPanel>
<TextBlock Text="{Binding Path=ToolPrototype.TypeName}" MaxWidth="200" FontWeight="Bold" TextWrapping="Wrap"></TextBlock>
<TextBlock Text="{Binding Path=ToolPrototype.Description}" MaxWidth="200" TextWrapping="Wrap"></TextBlock>
<TextBlock MaxWidth="200" Text="{Binding Path=ToolPrototype.Note}" FontWeight="Regular" FontStyle="Italic" TextWrapping="Wrap">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ToolPrototype.Note}" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</UserControl.ToolTip>
However, this does not build and says that the member "Visibility" is not recognized. Can anyone point out my mistake?
Upvotes: 1
Views: 1060
Reputation: 38199
Your Style
isn't associated with the TextBlock
type.
Consequently, WPF doesn't know about the Visibility
property. (Because that property is defined by TextBlock
)
You can explicitly tell WPF which class defines the property by changing it to TextBlock.Visibility
. Let me show an example:
<UserControl.ToolTip>
<StackPanel>
<TextBlock Text="{Binding Path=ToolPrototype.TypeName}" MaxWidth="200" FontWeight="Bold" TextWrapping="Wrap"></TextBlock>
<TextBlock Text="{Binding Path=ToolPrototype.Description}" MaxWidth="200" TextWrapping="Wrap"></TextBlock>
<TextBlock MaxWidth="200" Text="{Binding Path=ToolPrototype.Note}" FontWeight="Regular" FontStyle="Italic" TextWrapping="Wrap">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ToolPrototype.Note}" Value="{x:Null}">
<Setter Property="TextBlock.Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</UserControl.ToolTip>
Upvotes: 1