Tim Hibbard

CEO for EnGraph software
posts - 626, comments - 1586, trackbacks - 459

My Links

News



Add to Google

Twitter












Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

EnGraph Blogs

Links

Other

Roll

WPF – Hide a listbox when it doesn’t have any items

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: ,,

Print | posted on Tuesday, December 09, 2008 3:48 PM | Filed Under [ .NET WPF ]

Feedback

Gravatar

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

You can also use WPF built in BoolToVisibilityConverter and HasItems listbox property ;-)
8/30/2009 6:34 AM | Stéphane
Gravatar

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

True, that would be a better way now. At the time of this writing, that converter was not built into WPF
8/30/2009 4:33 PM | Tim Hibbard
Gravatar

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

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.
11/30/2009 10:38 PM | .Net Chick
Gravatar

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

Thank you for this post, i've been looking for this for a long time!
9/23/2010 2:13 PM | addominali
Gravatar

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

i've been looking for this for a long time!
1/31/2011 2:46 PM | dj nunti bacau
Gravatar

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

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..
4/22/2011 4:26 AM | nickel alloy pipe fittings
Gravatar

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

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.
4/22/2011 4:27 AM | nickel alloy pipe fittings
Gravatar

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

This is something, I always wanted to get. Hope to read more articles like this.
4/29/2011 7:12 AM | Pneumonia symptoms
Gravatar

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

This is a nice web site. Good fresh interface and nice informative articles. I will be coming back soon, thanks for the great article.
Gravatar

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

This is so nice. Got so much information here.
5/22/2011 6:06 AM | Government jobs
Gravatar

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


This is the perfect blog for anyone who wants to know about this topic.
5/31/2011 3:41 AM | chanel bags
Gravatar

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

Thanks for sharing this article! Its interesting and I am always searching for informative information like this.. Thanks a Lot again...
6/6/2011 8:00 AM | Generic Viagra Online
Gravatar

# generic

Great blog I am really interested your article ...Thanks for sharing ......
7/30/2011 5:48 AM | cheap viagra online
Gravatar

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

You did really a great job. I found your blog very interesting and very informative. thanks !!
8/1/2011 9:00 AM | online kamagra
Gravatar

# Re..

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.
8/8/2011 1:14 AM | generic viagra online
Gravatar

# Buy kamagra

Hi....
I am enjoy reading your article. use full information.nice post....
Kamagra
8/11/2011 7:30 AM | david
Gravatar

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

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.
8/11/2011 7:36 AM | Cheapest Generic Viagra
Gravatar

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

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...
8/29/2011 4:44 AM | womens leather jackets
Gravatar

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

Hi..Thanks a lot for sharing the code for WCF..I was looking for the same application
9/3/2011 1:55 AM | cheap generic viagra
Gravatar

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

This is really great to see. Thanks for sharing this post with people out here.
9/15/2011 7:09 AM | Generic Flagyl
Gravatar

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

This will not have effect in reality, that is what I believe.
9/16/2011 4:06 PM | muebles madrid
Gravatar

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

Hi Friends...,
what you have write and shared here is really great..I think you did a fantastic job by sharing your thoughts here.
9/27/2011 11:22 AM | Buy Generic Viagra
Gravatar

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

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
10/12/2011 5:05 PM | learn spanish on dvd
Gravatar

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

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
10/31/2011 1:07 PM | Buy Generic Viagra
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: