locked
How do I make a control visible in Blend if the Visibility property is data bound? RRS feed

  • Question

  • Hello,

    I have a grid that shows a list of parts. When I click on an item in the grid, another grid is made visible that shows information about the selected part. For example, if the part is a PC, then the information shown would be things like MAC address, serial number, etc.

    I have set the Visibility property of the second grid to be data bound to the SelectedItems.Count property of the first grid, using a value converter that converts the number of selected items to a string that represents visibility.

    This all works very nicely at run time, but as the first grid doesn't have a selected item at design time (it doesn't even have any data in it), the second grid is collapsed, making it very difficult to work with.

    How can I have the second grid visible at design time, but keep my run time behaviour?

    Thanks


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Sunday, February 20, 2011 6:49 PM

Answers

  • Hello,

    to achieve this, in the value converter you have to write this code:

    if (DesignerProperties.GetIsInDesignMode(Application.Current.MainWindow))
                    return Visibility.Visible;

    I hope it helps you.

     


    Sergio Garrido. http://playsilverlight.wordpress.com
    • Marked as answer by Mr Yossu Monday, February 21, 2011 2:15 PM
    Sunday, February 20, 2011 10:02 PM

All replies

  • Hello,

    to achieve this, in the value converter you have to write this code:

    if (DesignerProperties.GetIsInDesignMode(Application.Current.MainWindow))
                    return Visibility.Visible;

    I hope it helps you.

     


    Sergio Garrido. http://playsilverlight.wordpress.com
    • Marked as answer by Mr Yossu Monday, February 21, 2011 2:15 PM
    Sunday, February 20, 2011 10:02 PM
  • Thanks Sergio, that was it!
    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Monday, February 21, 2011 2:16 PM
  • Hello again,

    I've just discovered that as soon as I add this converter using Blend (instead of writing the XAML by hand in VS), Blend crashes.

    It does this consistently, on whichever control I use it.

    Any ideas? Thanks again


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Tuesday, February 22, 2011 6:22 PM
  • Hello,

    try using this code:

    if
    (DesignerProperties
    .GetIsInDesignMode(new
     DependencyObject
    ()))
    return Visibility .Visible;

    It could be that there are problems with Application.Current.MainWindow property, but I haven't had any problems in Blend 4.

    I hope it solves your problem.

     


    Sergio Garrido. http://playsilverlight.wordpress.com
    • Edited by Sergio Garrido Tuesday, February 22, 2011 7:10 PM code indentation
    Tuesday, February 22, 2011 7:08 PM
  • Ah, here e have an interesting issue, where I posted the problem to completely the wrong place!

    Actually, the code you supplied works beautifully. The problem I had came with a special converter I wrote (or copied from here actually) that allows you to debug data binding. I must have been day dreaming when I posted my reply, as I posted it here, where we had been discussing converters.

    Sorry for the confusion, but thanks for the reply.


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Tuesday, February 22, 2011 8:38 PM
  • you're welcome!
    Sergio Garrido. http://playsilverlight.wordpress.com
    Tuesday, February 22, 2011 8:43 PM