locked
Using Collapse to Collapse a TextBlock in Windows Store App project RRS feed

  • Question

  • I need for my TextBlock to collapse if there's no text, and there's many examples of doing this on the web in WPF (such as the one below). If I put the trigger code below in my Windows Store App project, the compile tells me "Unknown member 'Triggers' on element 'Style'.

    If I try to set my TextBlock Visibility property to Windows.UI.Xaml.Visibility.Collapsed in the code-behind, it has no effect, and the empty TextBlock continues to be visible. You can see how I've set up a Grid with a TextBlock in the second code sample below.

    Any ideas? I really need for the TextBlock to disappear when it is empty.

    Thanks...

    <Style x:Key="CollapsableTextblock" TargetType="TextBlock"> 
        <Style.Triggers> 
            <Trigger Property="Text" Value=""> 
                <Setter Property="Visibility" Value="Collapsed"/> 
            </Trigger> 
        </Style.Triggers> 
    </Style> 
    <TextBlock Text="" Style="{DynamicResource CollapsableTextblock}" />
    

    <Grid x:Name="gridMain">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
    
        <TextBlock x:Name="txtReport"/>
        <local:MapBing Grid.Column="1" />
    </Grid>


    Randy

    Tuesday, December 10, 2013 9:45 PM

All replies

  • The error is correct. You cannot use WPF specific code in Windows Store apps. You can check the documentation or use the Object Browser in Visual Studio to see what classes and methods are available for your solution.

    Setting the TextBlock's visibility to Collapsed programmatically should work just fine and does in my test. The TextBlock doesn't have a TextChanged event so you cannot tap into that directly. You should be able to bind the block's Visibility to the underlying text source.

    --Rob

    Tuesday, December 10, 2013 11:50 PM
    Moderator
  • You might want to try to bind the Visibility to the Text property and compute the value using a converter

    Visibility="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource StringToVisibility}"

    You just need to create the StringToVisibility converter.


    Client Application Development MVP

    • Proposed as answer by Worker Role Thursday, December 12, 2013 3:10 PM
    Tuesday, December 10, 2013 11:56 PM
  • If you created a small test, perhaps you could post your code. I found that setting Visibility = Collapsed in my code-behind constructor did not change the Visibility value.

    Thanks...


    Randy

    Wednesday, December 11, 2013 10:28 AM
  • My TextBlock is defined in XAML as follows:

    <TextBlock x:Name="txtReport" Grid.Column="0"/>

    Just for testing, I put the following in my C# code-behind constructor:

    txtReport.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

    I put a breakpoint on the line that sets Visibility and hit F5. The assignment above does not cause the value of the Visibility variable to change. It retains the value of Visible, and of course, I continue to see the TextBlock.

    I can try writing a converter - it's not hard to do. I'm still trying to figure out why setting the Visibility property doesn't work in the context above.


    Randy

    Wednesday, December 11, 2013 10:09 PM
  • I'm not sure why that's not working for you. Your code snippets work for me if I put the txtReport.Visibility line either in my page's constructor right after InitializeComponent or in a button Click handler.

    I suspect you have something elsewhere in your code which is interfering with this. If you need further help please share a minimal repro sample on your SkyDrive so we can see the problem in action.

    --Rob

    Thursday, December 12, 2013 1:21 AM
    Moderator
  • Randy, I'd propose Ben's answer as ideal, it does, however, require you using the MVVM pattern ~ which is highly recommended. The converter he's talking about is an IValueConverter, they are bread and butter stuff hence if you are not into MVVM I'd recommend you jump headfirst into it!
    • Edited by Worker Role Thursday, December 12, 2013 3:13 PM
    Thursday, December 12, 2013 3:12 PM