Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wpf

xaml

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.

like image 792
ProfK Avatar asked Jan 30 '17 05:01

ProfK


2 Answers

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

like image 62
Sisir Avatar answered Sep 17 '22 08:09

Sisir


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.

like image 21
RandomEngy Avatar answered Sep 17 '22 08:09

RandomEngy