0

I created a TabControl whose Header's contents are two TextBlocks: one labeling the Header with a name, the other showing a symbol font. Depending on the selected TabItem I want to show the Header text in the first TextBlock, while collapsing it, as soon as another TabItem is selected (therefore showing the Header text of the now selected TabItem).

E.g.: Selecting TabItem1 shows TextBlock1 with the Header text, TextBlock2 is showing a symbol font. Selecting TabItem2 collapses the TextBlock1 Visibility, and therefore sets the first TextBlock's Visibility of TabItem2 form Collapsed to Visible.

I would like to try a xaml-solution; with code behind I can achieve the desired functionality. I guess I would need some trigger, but I am obviously too unexperienced with xaml to implement them properly.

Here is what I got so far. Please note that my code does show the expected behaviour for the first time, but then Visibility of the TextBlock is always set to "collapsed". Here's my code:

<TabItem x:Name="Item2" GotFocus="Item2_GotFocus" LostFocus="Item2_LostFocus">                    
                <TabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock x:Name="Text2" FontSize="14" FontFamily="Quicksand" VerticalAlignment="Center" Content="TestEntry">
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding ElementName=Item2, Path=IsFocused}" Value="True">
                                            <Setter Property="Visibility" Value="Visible"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>                          
                        </TextBlock>
                        <TextBlock Margin="20,0,0,0" FontSize="32" FontFamily="APPFONT" VerticalAlignment="Center">B</TextBlock>
                    </StackPanel>
                </TabItem.Header>
            </TabItem>

By starting the app I get to see both TextBlocks; as soon as another TabItem is chosen, the first TextBlock's visibility is set to collapsed. Clicking again on this TabItem shows the first TextBlock not again.

7
  • Did you try binding to IsSelected instead of IsFocused? Commented Jun 20, 2019 at 17:49
  • Yes I did - but they seem interchangebly unfortunatelly. Same as with IsFocused. Commented Jun 20, 2019 at 18:52
  • They're not interchangeable. Both work fine for me, btw. I couldn't reproduce the problem you're having with the XAML you provided. Is this really WPF? You have a TextBlock with a Content property. That won't compile in WPF. Commented Jun 20, 2019 at 18:53
  • @ content property: sorry, my bad - I deleted the original test, filled in content by hand after releasing the code here: its TEXT property of course. And yes, it is wpf. You said it worked fine for you? So the the TextBlock appeared every time you went back to that specific TabItem? Commented Jun 20, 2019 at 19:52
  • 1
    OMG - this is so embarassing! It DOES work! I forgot about the event handlers - they totally messed up everything! I'm trying for two days now, and could not find the most obvious of mistakes! So, my xaml-code does work - it's been the event handlers, fireing an unsuitable event in my code, resetting all to "collapsed"! Thank you, Ed! Commented Jun 20, 2019 at 21:08

0

Browse other questions tagged or ask your own question.