Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3841648
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:40:56+00:00 2026-05-19T15:40:56+00:00

I have a collection databound to a ListBox . What I would like to

  • 0

I have a collection databound to a ListBox. What I would like to do is show some UI based on whether or not some property of the member of the collection exists.

E.g.:

public class Widget
{
  public string foo;
  public string bar;
}

public ObservableCollection<Widget> Stuff;

XAML:

<ListBox ItemsSource="{Binding Stuff}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding foo}" 
                 Visiblity="{Binding 
                   (foo != null ? Visibility.Visible : Visibility.Collapsed)
                 }"/>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

Note the Visibility attribute on the TextBlock. Clearly this isn’t supported, but it should give you an idea of what I want to do.

One possible solution is that I could add a property to widget that looks like this:

public Visibility has_foo;

And then:

... Visibility="{Binding has_foo}" ...

But it seems awkward to have to generate these additional properties.

I suspect there is a much better way. Is there? How would you do it?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:40:56+00:00Added an answer on May 19, 2026 at 3:40 pm

    Create a value converter. Something like

    public class NullToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value != null ? Visibility.Visible : Visibility.Collapsed;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Then add it something like

    <YourUserControl.Resources>
       <NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
    </YourUserControl.Resources>
    
    <ListBox ItemsSource="{Binding Stuff}">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding foo}" 
                     Visiblity="{Binding foo, 
                         Converter={StaticResource NullToVisibilityConverter}"/>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
    

    Obviously I’ve not implemented ConvertBack (not really sure if you will be able to convert back) but you shouldn’t need it in this instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Listbox with its ItemsSource property databound to a collection of objects.
I have a listbox that is databound to a Collection of objects. The listbox
I have a listbox which is databound to a collection of objects. I want
for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}
I have a Collection View Source (CVS) implemented much like you see in MSDN
I have a collection with documents like this {userId: 123, profile: [{prof1:p}, {prof2 :
I have a collection of objects to which I'd like to just add a
I have a TreeView databound to a CollectionViewSource Groups collection. This is so that
I have a ListBox which is databound to an ObservableCollection and has a complex
Is there a way to have collection of objects which do not have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.