Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software

This code example will show how to hide a listbox from the user when it doesn’t have any items. 

It involves binding the Visibility of the listbox to the Items.Count of the listbox.  We run the Items.Count through a converter that will return a Visibility.Visible object if the Item.Count is not zero.

Here is the converter class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;
 
namespace HideAListBox
{
    public class ZeroCollapsedNonZeroVisible : IValueConverter
    {
 
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var rv = Visibility.Visible;
            var val = 0;
            int.TryParse(value.ToString(), out val);
            if (val == 0)
            {
                rv = Visibility.Collapsed;
            }
            return rv;
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
 
    }
}

Here is the window code:

<Window x:Class="HideAListBox.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:converter="clr-namespace:HideAListBox"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <converter:ZeroCollapsedNonZeroVisible x:Key="hideListBox"/>
    </Window.Resources>
    <DockPanel LastChildFill="True">
        <Button Content="Add Item" Click="addItemClick" DockPanel.Dock="Top"/>
        <Button Content="Clear Items" Click="clearItemsClick" DockPanel.Dock="Top"/>
        <ListBox Margin="3" BorderBrush="Blue" Name="myListBox" 
                 Visibility="{Binding ElementName=myListBox, 
                              Path=Items.Count, 
                              Converter={StaticResource hideListBox}}"/>
    </DockPanel>
</Window>

The entire project can be downloaded here.

Technorati Tags: ,,
Posted on Tuesday, December 9, 2008 3:48 PM .NET , WPF | Back to top


Comments on this post: WPF – Hide a listbox when it doesn’t have any items

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
You can also use WPF built in BoolToVisibilityConverter and HasItems listbox property ;-)
Left by Stéphane on Aug 30, 2009 6:34 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
True, that would be a better way now. At the time of this writing, that converter was not built into WPF
Left by Tim Hibbard on Aug 30, 2009 4:33 PM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
Thank you so much for your post you have saved me hours of work. I have been looking all over for a way to do this and this worked perfectly for me.
Left by .Net Chick on Nov 30, 2009 10:38 PM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
Thank you for this post, i've been looking for this for a long time!
Left by addominali on Sep 23, 2010 2:13 PM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
Wow, nice post,there are many person searching about that now they will find enough resources by your post.Thank you for sharing to us.Please one more post about that..
Left by nickel alloy pipe fittings on Apr 22, 2011 4:26 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
For each cup of uncooked rice you'll need about 4 cups of hot chicken stock. Keep the stock hot in a small saucepan at a low simmer over a separate burner from the one you'll be using to cook the risotto. You'll also need a small (e.g. 6 oz.) ladle for adding the hot stock.

Tip: Use a wooden spoon for stirring the risotto — it's less likely to break the grains of rice than a metal spoon.
Left by nickel alloy pipe fittings on Apr 22, 2011 4:27 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
This is something, I always wanted to get. Hope to read more articles like this.
Left by Pneumonia symptoms on Apr 29, 2011 7:12 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
This is a nice web site. Good fresh interface and nice informative articles. I will be coming back soon, thanks for the great article.
Left by send mothers day flowers Hyderab on May 06, 2011 3:26 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
This is so nice. Got so much information here.
Left by Government jobs on May 22, 2011 6:06 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...

This is the perfect blog for anyone who wants to know about this topic.
Left by chanel bags on May 31, 2011 3:41 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
Thanks for sharing this article! Its interesting and I am always searching for informative information like this.. Thanks a Lot again...
Left by Generic Viagra Online on Jun 06, 2011 8:00 AM

# generic
Requesting Gravatar...
Great blog I am really interested your article ...Thanks for sharing ......
Left by cheap viagra online on Jul 30, 2011 5:48 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
You did really a great job. I found your blog very interesting and very informative. thanks !!
Left by online kamagra on Aug 01, 2011 9:00 AM

# Re..
Requesting Gravatar...
Your post is extremely informative and well written. I am also happy with the way you present it with points and bullets. It is so much easier to understand whatever you want to bring across. Thank you.
Left by generic viagra online on Aug 08, 2011 1:14 AM

# Buy kamagra
Requesting Gravatar...
Hi....
I am enjoy reading your article. use full information.nice post....
Kamagra
Left by david on Aug 11, 2011 7:30 AM

# # re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
It's awesome blog post here.... really very interesting for reading..... I enjoyed it. Thanks for the share.... just keep posting such an informative articles, I want to know more about this topic.
Left by Cheapest Generic Viagra on Aug 11, 2011 7:36 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
I genuinely love how it is easy on my eyes as well as the details are well written. I am wondering how I can be notified whenever a new post has been made. I have subscribed to your rss feed which must do the trick! Have a nice day...
Left by womens leather jackets on Aug 29, 2011 4:44 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
Hi..Thanks a lot for sharing the code for WCF..I was looking for the same application
Left by cheap generic viagra on Sep 03, 2011 1:55 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
This is really great to see. Thanks for sharing this post with people out here.
Left by Generic Flagyl on Sep 15, 2011 7:09 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
This will not have effect in reality, that is what I believe.
Left by muebles madrid on Sep 16, 2011 4:06 PM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
Hi Friends...,
what you have write and shared here is really great..I think you did a fantastic job by sharing your thoughts here.
Left by Buy Generic Viagra on Sep 27, 2011 11:22 AM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
Get Enjoy with your Kids and Family:
Box Set: 10 DVDs was $119.95, SALE $99.95
Level 1: 3 DVDs was $44.95, SALE $34.95
Level 2: 3 DVDs was $44.95, SALE $34.95
Level 3: 4 DVDs was $44.95, SALE $39.95
Left by learn spanish on dvd on Oct 12, 2011 5:05 PM

# re: WPF – Hide a listbox when it doesn’t have any items
Requesting Gravatar...
I would like read more information about this, is very interesting! Thanks for the information. A worth bookmarking blog. I would be reading your articles regularly from now on.
Buy Generic viagra
Left by Buy Generic Viagra on Oct 31, 2011 1:07 PM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net