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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:08:31+00:00 2026-05-26T11:08:31+00:00

Before any item in a ComboBox is selected, its SelectedItem is null and the

  • 0

Before any item in a ComboBox is selected, its SelectedItemis null and the ComboBox itself is visually blank. Once something is selected, there doesn’t seem to be any way for the user to select “the absence of a selection” (though it can be done by setting SelectedItem to null in code).

My ComboBoxes are bound to ObservableCollections of my objects. I don’t want to add a “special” first null-like object to the front of every ObservableCollection. So I’m taking this opportunity to learn a bit about writing a UserControl.

The problem is SelectedItem doesn’t work the way it normally does. That is, the ComboBox is nicely bound to a backing ObservableCollection, but picking something from the ComboBox doesn’t update the SelectedItem it’s supposed to be bound to.

I feel like I need to be passing along some information from the ComboBox in the UserControl to…somewhere. Am I on the right track? What should I be googling for?

C#:

public partial class ClearableComboBox : UserControl
{
    public ClearableComboBox()
    {
        InitializeComponent();
    }

    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)base.GetValue(ItemsSourceProperty); }
        set { base.SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource",
            typeof(IEnumerable),
            typeof(ClearableComboBox));

    public object SelectedItem
    {
        get { return (object)base.GetValue(SelectedItemProperty); }
        set { base.SetValue(SelectedItemProperty, value); }
    }

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem",
            typeof(object),
            typeof(ClearableComboBox));

    public string DisplayMemberPath
    {
        get { return (string)base.GetValue(DisplayMemberPathProperty); }
        set { base.SetValue(DisplayMemberPathProperty, value); }
    }

    public static readonly DependencyProperty DisplayMemberPathProperty =
        DependencyProperty.Register("DisplayMemberPath",
            typeof(string),
            typeof(ClearableComboBox));

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        comboBox.SelectedItem = null;
    }
}

XAML:

<UserControl x:Class="MyProj.ClearableComboBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             x:Name="root">
    <DockPanel>
        <Button DockPanel.Dock="Left" Click="Button_Click" ToolTip="Clear">
            <Image Source="pack://application:,,,/img/icons/silk/cross.png" Stretch="None" />
        </Button>
        <ComboBox
            Name="comboBox"
            ItemsSource="{Binding ElementName=root, Path=ItemsSource}"
            SelectedItem="{Binding ElementName=root, Path=SelectedItem}"
            DisplayMemberPath="{Binding ElementName=root, Path=DisplayMemberPath}" />
    </DockPanel>
</UserControl>

Usage:

<wpfControl:ClearableComboBox ItemsSource="{Binding Path=Things}"
                              DisplayMemberPath="SomeProperty"
                              SelectedItem="{Binding Path=SelectedThing}" />

// Picking a Thing doesn't update SelectedThing :(
  • 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-26T11:08:31+00:00Added an answer on May 26, 2026 at 11:08 am

    Since combobox derives from Selector class which in turn derives from ItemsControl. So, by deriving from UserControl you are devoiding your combobox with properties of Selector class which might internally handle the Selection thing for you. so, i would suggest instead of deriving it from UserControl, you should derive it from Combobox like this –

    public partial class ClearableComboBox : ComboBox
    

    So, that ways you won’t have to override the ItemsSource, DisplayMemberPath etc. in your class since it s already present in the ComboBox class. You can always extend your class further to provide addidtional features which is in your case setting the SelectedItem to null on some button click. Hope this is what you want..

    EDIT (Custom Control)

    Creating a Custom Control is your answer here, to get started if you are not aware of it, look at this for start – http://www.wpftutorial.net/HowToCreateACustomControl.html

    When you create a Custom Control say CustomControl1, replace the template for CustomControl1 in your Generic.xaml file with this one –

    <ControlTemplate TargetType="{x:Type local:CustomControl1}">
        <Border Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}">
                  <DockPanel>
                     <Button Name="btn" DockPanel.Dock="Left" ToolTip="Clear" Width="20">
                        <Image Source="pack://application:,,,/img/icons/silk/cross.png" Stretch="None" />
                    </Button>
                    <ComboBox Name="comboBox"
                              ItemsSource="{TemplateBinding ItemsSource}"
                              SelectedItem="{TemplateBinding SelectedItem}"
                              DisplayMemberPath="{TemplateBinding DisplayMemberPath}" />
                  </DockPanel>
         </Border>
    </ControlTemplate>
    

    By default your CustomControl1 class will be derived from Control. Replace it to derive from class ComboBox so that you don’t have declare DP’s yet over again like this and copy paste this code there –

    public class CustomControl1 : ComboBox
    {
            private Button clearButton;
            private ComboBox comboBox;
    
            static CustomControl1()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
            }
    
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
    
                clearButton = GetTemplateChild("btn") as Button;
                comboBox = GetTemplateChild("comboBox") as ComboBox;
                clearButton.Click += new RoutedEventHandler(clearButton_Click);
            }
    
            private void clearButton_Click(object sender, RoutedEventArgs e)
            {
                comboBox.SelectedItem = null;
            }
    }
    

    Now, your CustomControl1 class is ready for use in your other xaml files like this –

    <local:CustomControl1 ItemsSource="{Binding YourSource}"
                          SelectedItem="{Binding YourSelectedItem}"
                          Height="50" Width="200"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has any one done this before? It would seem to me that there should
Is there any replacement for saxon:if and saxon:before functions in XSLT 2.0 / XPath
Is there any way of locking (disabling) a list item while a workflow is
I've never done any item delegates in Qt before, and I think the documentation
Is there any in-built technique for moving straight to the last UIViewController shown before
MySQL ResultSets are by default retrieved completely from the server before any work can
I need to tweak some things inside Tomcat 6.0 before any web applications or
Here's a simplified version of what I'm trying to do : Before any other
Is hashing a password twice before storage any more or less secure than just
One thing I struggle with is planning an application's architecture before writing any code.

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.