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 8597319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:53:14+00:00 2026-06-12T00:53:14+00:00

I am using a standard wpf/mvvm application where i bind combo boxes to collections

  • 0

I am using a standard wpf/mvvm application where i bind combo boxes to collections on a ViewModel.

I need to be able to de-select an item from the dropdown. Meaning, users should be able to select something, and later decide that they want to un-select it (select none) for it. the problem is that there are no empty elements in my bound collection

my initial thought was simply to insert a new item in the collection which would result having an empty item on top of the collection.

this is a hack though, and it affects all code that uses that collection on the view model.

for example if someone was to write

_myCollection.Frist(o => o.Name == "foo") 

this will throw a null reference exception.

possible workaround is:

_myCollection.Where(o => o != null).First(o => o.Name == "foo");

this will work, but no way to ensure any future uses of that collection won’t cause any breaks.

what’s a good pattern / solution for being able to adding an empty item so the user can de-select. (I am also aware of CollectionView structure, but that seems like a overkill for something so simple)

Update

went with @hbarck suggestion and implemented CompositeCollection (quick proof of concept)

    public CompositeCollection MyObjects {
        get {
            var col = new CompositeCollection();

            var cc1 = new CollectionContainer();
            cc1.Collection = _actualCollection;

            var cc2 = new CollectionContainer();
            cc2.Collection = new List<MyObject>() { null }; // PROBLEM

            col.Add(cc2);
            col.Add(cc1);
            return col;
        }
    }

this code work with existing bindings (including SelectedItem) which is great.

One problem with this is, that if the item is completely null, the SelectedItem setter is never called upon selecting it.

if i modify that one line to this:

            cc2.Collection = new List<MyObject>() { new MyObject() }; // PROBLEM

the setter is called, but now my selected item is just a basic initialized class instead of null.. i could add some code in the setter to check/reset, but that’s not good.

  • 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-06-12T00:53:15+00:00Added an answer on June 12, 2026 at 12:53 am

    I think the easiest way would be to use a CompositeCollection. Just append your collection to another collection which only contains the empty item (null or a placeholder object, whatever suites your needs), and make the CompositeCollection the ItemsSource for the ComboBox. This is probably what it is intended for.

    Update:

    This turns out to be more complicated than I first thought, but actually, I came up with this solution:

    <Window x:Class="ComboBoxFallbackValue"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:t="clr-namespace:TestWpfDataBinding"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        xmlns:w="clr-namespace:System.Windows;assembly=WindowsBase"
    Title="ComboBoxFallbackValue" Height="300" Width="300">
    <Window.Resources>
        <t:TestCollection x:Key="test"/>
        <CompositeCollection x:Key="MyItemsSource">
            <x:Static Member="t:TestCollection.NullItem"/>
            <CollectionContainer Collection="{Binding Source={StaticResource test}}"/>
        </CompositeCollection>
        <t:TestModel x:Key="model"/>
        <t:NullItemConverter x:Key="nullItemConverter"/>
    </Window.Resources>
    <StackPanel>
        <ComboBox x:Name="cbox" ItemsSource="{Binding Source={StaticResource MyItemsSource}}" IsEditable="true" IsReadOnly="True" Text="Select an Option" SelectedItem="{Binding Source={StaticResource model}, Path=TestItem, Converter={StaticResource nullItemConverter}, ConverterParameter={x:Static t:TestCollection.NullItem}}"/>
        <TextBlock Text="{Binding Source={StaticResource model}, Path=TestItem, TargetNullValue='Testitem is null'}"/>
    </StackPanel>
    

    Basically, the pattern is that you declare a singleton NullInstance of the class you use as items, and use a Converter which converts this instance to null when setting the VM property. The converter can be written universally, like this (it’s VB, I hope you don’t mind):

    Public Class NullItemConverter
    Implements IValueConverter
    
    Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        If value Is Nothing Then
            Return parameter
        Else
            Return value
        End If
    End Function
    
    Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        If value Is parameter Then
            Return Nothing
        Else
            Return value
        End If
    End Function
    

    End Class

    Since you can reuse the converter, you can set this all up in XAML; the only thing that remains to be done in code is to provide the singleton NullItem.

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

Sidebar

Related Questions

I have a C# WPF application using a rather noddy MVVM approach. In one
I am developing a WPF application (using MVVM) and have implemented IDataErrorInfo on my
What I have so far is: A WPF application, using MVVM, IDataErrorInfo implemented. Everything
I'm trying to put standard output from nmap into WPF window application (textbox exactly).
I am using standard WPF DataGrid. When user double clicks (or F2) on some
Let me clarify: I'm using standard jQuery autocomplete plugin (bassistanse.de) and bind it to
I have an application running under WebLogic that is using standard forms authentication. The
I have a WPF application running with VS2010 .Net3.5 using Nhibernate with FluentNHibernate +
I have been using ItemsControl for quiet sometime in WPF. I am using MVVM
We are developing a completely standard WPF application with a menu and toolbar. Both

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.