Friday, February 10, 2017

Why in my WPF view is the Content text of my Label control not visible?

Leave a Comment

I have the following markup:

<StackPanel Grid.Row="0" Orientation="Horizontal">     <StackPanel Orientation="Horizontal" Visibility="{Binding OrgListVisibility}">         <Label Content="Org:" />         <ComboBox ItemsSource="{Binding OrgSelectList, NotifyOnSourceUpdated=True}" SelectedValuePath="Key" DisplayMemberPath="Value" SelectedItem="{Binding OrgId}" />     </StackPanel>     <StackPanel Orientation="Horizontal" Visibility="{Binding BranchListVisibility}">         <TextBlock Text="Branch:" Style="{StaticResource FormLabel}" />         <ComboBox x:Name="BranchList" ItemsSource="{Binding BranchSelectList}" SelectedValuePath="Key" DisplayMemberPath="Value" SelectedItem="{Binding BranchId}" />     </StackPanel> </StackPanel> 

Yet when I run the app, only the text from the TextBlock is visible, and not that of the Label. The latter is in the Visual Tree, with a TextBlock deep down, but that is as far as I can see.

AS REQUESTED: Here is the style for FormLabel:

<Style TargetType="TextBlock" x:Key="FormLabel">     <Setter Property="Height" Value="20" />     <Setter Property="Margin" Value="10" />     <Setter Property="TextAlignment" Value="Right" />     <Setter Property="VerticalAlignment" Value="Center" /> </Style> 

A SIMILAR PROBLEM: I found an almost similar problem with a combobox when I bound it to a collection of instances of a generic class. The items' text simply did not show, but they were present in the comboboxes. Selecting on the one by knowing the position of my sought item correctly cascaded to the 2nd combobox, which had visible items, and I could see the correct but invisible item had been selected.

As soon as I change the item source to a list of non-generic objects, the items in the dropdown were visible again.

4 Answers

Answers 1

The code looks fine and as you have mentioned in the comments section that it takes layout space then it may very well happen that the color of your label and the background color of the containing layout be same.

To troubleshoot this, try giving some different background and foreground colors e.g. red or blue to the Label. Hope this helps

Answers 2

The Label would take up layout space while not being visible when its Visibility == Hidden. You should check and make sure that your application does not define a global style (one with no Key) for TargetType="Label" where this value could be set:

<Style TargetType="Label"> !!!note that this has no 'Key' associated       [...]     <Setter Property="Visibility" Value="Hidden" />       [...] </Style> 

This would not need to be in the same xaml file in order to be automatically applied, you should check the global dictionary or any other ResourceDictionary linked in the file.

Answers 3

Ctrl+Q -> Live Visual Tree

Then hit the "pick element" button and select your label. Check the following properties:

Visibility
Opacity
Content

Also check the child elements of the Label. Setting the Content should result in a tree like this:

Label visual tree

If a default style has changed the control template, you might not see the TextBlock as a child here. Also drill into the TextBlock and make sure it has the right Text property, then make sure it and all its parents have the right Opacity and Visibility . Also make sure that the inner TextBlock has space allocated to it by selecting it and turning on the highlighting feature in the live visual tree window.

Answers 4

Can you try this code to see if it works?

 <Grid Grid.Row="0">     <Grid.ColumnDefinitions>         <ColumnDefinition Width="Auto"/>         <ColumnDefinition Width="Auto"/>     </Grid.ColumnDefinitions>      <StackPanel Grid.Column="0"                 Orientation="Horizontal" Visibility="{Binding OrgListVisibility}">         <Label Content="Org:" />         <ComboBox ItemsSource="{Binding OrgSelectList, NotifyOnSourceUpdated=True}"                        SelectedValuePath="Key"                        DisplayMemberPath="Value"                        SelectedItem="{Binding OrgId}" />     </StackPanel>     <StackPanel Grid.Column="1"                 Orientation="Horizontal" Visibility="{Binding BranchListVisibility}">         <TextBlock Text="Branch:" Style="{StaticResource FormLabel}" />         <ComboBox x:Name="BranchList"                        ItemsSource="{Binding BranchSelectList}"                        SelectedValuePath="Key"                        DisplayMemberPath="Value"                        SelectedItem="{Binding BranchId}" />     </StackPanel> </Grid> 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment