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

  • Home
  • SEARCH
  • 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 163303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:32:38+00:00 2026-05-11T11:32:38+00:00

I am binding a WPF ComboBox to a nullable property of type MyEnum? (where

  • 0

I am binding a WPF ComboBox to a nullable property of type MyEnum? (where MyEnum is an enumerated type)

I am programmatically populating the ComboBox items like this:

// The enum type being bound to  enum MyEnum { Yes, No }  // Helper class for representing combobox listitems // (a combination of display string and value) class ComboItem {   public string Display {get;set}   public MyEnum? Value {get;set} }  private void LoadComboBoxItems() {   // Make a list of items to load into the combo   var items = new List<ComboItem> {     new ComboItem {Value = null, Display = 'Maybe'},     new ComboItem {Value = MyEnum.Yes, Display = 'Yes'},     new ComboItem {Value = MyEnum.No, Display = 'No'},};    // Bind the combo's items to this list.   theCombo.ItemsSource = items;   theCombo.DisplayMemberPath = 'Display';   theCombo.SelectedValuePath = 'Value'; } 

Also in the code-behind, I am setting the DataContext to an instance of a class with a property called TheNullableProperty (for this example anyway) of type MyEnum?.

The binding of theCombo’s SelectedValue is done in my XAML file.

<ComboBox    Name='theCombo'    SelectedValue='{Binding Path=TheNullableProperty,                           UpdateSourceTrigger=PropertyChanged}'/> 

Problem:

When the value of the bound property is initially non-null, the combo box displays the value properly.

But when the value of the bound property is initially null, the combo box is blank.

It looks like every aspect of data binding is working apart from the representation of the null value when the combobox is first shown.

For example: you can select Maybe from the dropdown, and the bound property is correctly set to null. It’s just that initial loading that’s failing. Maybe I need to just manually set the SelectedValue initially…

What I Ended Up Doing

  • Add a hidden textblock databound to the underlying nullable enum value via a Converter that converts from the nullable enum to a string (enum.ToString, or ‘null’).
  • Load up the combo box with ‘ComboItems’ each having a string Label (displayed in the combo) and a string Value equal to the enum values as strings (and ‘null’ for the null value).
  • Data-bind the combo box to the textblock.

    /// <summary> /// Convert from EnumeratedType? to string (null->'null', enum values->ToString) /// </summary> public class EnumConverter<T> : IValueConverter where T:struct  {   public static string To(T? c)   {     if (c == null)       return 'null';     return c.ToString();   }    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)   {     return To((T?)value);   }    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)   {     var s = (string) value;     if (s == 'null')       return null;     return (T?)Enum.Parse(typeof(T), s);   } }  public class MyEnumConverter : EnumConverter<MyEnum> { }  public class ComboItem {   public string Value { get; set; }   public string Label { get; set; }    public ComboItem(MyEnum? e, string label)   {     Value = MyEnumConverter.To(e);     Label = label;   } }  static IEnumerable<ComboItem> GetItems() {   yield return new ComboItem(null, 'maybe');   yield return new ComboItem(MyEnum.Yes, 'yup');   yield return new ComboItem(MyEnum.No, 'nope'); }  private void SetupComboBox() {   thecombo.ItemsSource = GetItems().ToList(); } 
  • 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. 2026-05-11T11:32:38+00:00Added an answer on May 11, 2026 at 11:32 am

    You cannot bind to a null value by design in WPF (at least 3.5 SP1). This means that at the moment Source gets a Null as a value your Binding will be automatically broken and won’t work even if you specify a valid value for the Source. You need to provide some ‘heart beating’ mechanism for your binding via Value Converter. So that Converter transforms null value to some ‘Undefined’ (non-null) for the Targets and than is able to convert your ‘Undefined’ back to null…

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

Sidebar

Related Questions

For those who like a good WPF binding challenge: I have a nearly functional
I'm getting all learned up about binding in WPF. I'm having a lot of
I have a situation where I am using wpf data binding and validation using
I'm new to WPF and data binding so hopefully I can explain the problem
I have a very simple WPF application in which I am using data binding
I know that WPF 3.5 SP1 supports a StringFormat in a binding, but can
I'm working through the Data Binding chapter in Pro WPF in C# 2008. They
I'm VERY new to WPF, and still trying to wrap my head around binding
I have a WPF ListView that is bound to a BindingList<T>. The binding works
How do I use RelativeSource with WPF bindings and what are the different use-cases?

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.