7

I would like to display a default text in the combobox. For example, "Choose a person" message. could you please help me out.

Please note that I am using databinding from domaincontext

Thank you !!

Isaaac_Perlman
  • 322
  • 5
  • 19

3 Answers3

13

To achieve this, I used a derived ExtendedComboBox class which extends the built-in ComboBox class. You can find the source code of this class in my blog post or below.

After you add this class to your project, you can use this XAML code to display a default value:

<local:ExtendedComboBox ItemsSource="{Binding ...Whatever...}" NotSelectedText="Select item..." />

Also, here is the test page with this control. I think that the second combobox is that what you need. example of the extended ComboBox

Full code of this class:

[TemplateVisualState(Name = ExtendedComboBox.StateNormal, GroupName = ExtendedComboBox.GroupItemsSource)]
[TemplateVisualState(Name = ExtendedComboBox.StateNotSelected, GroupName = ExtendedComboBox.GroupItemsSource)]
[TemplateVisualState(Name = ExtendedComboBox.StateEmpty, GroupName = ExtendedComboBox.GroupItemsSource)]
public class ExtendedComboBox : ComboBox
{
    public const string GroupItemsSource = "ItemsSourceStates";
    public const string StateNormal = "Normal";
    public const string StateNotSelected = "NotSelected";
    public const string StateEmpty = "Empty";

    private ContentPresenter selectedContent;

    public ExtendedComboBox()
    {
        this.DefaultStyleKey = typeof(ComboBox);
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        this.selectedContent = this.GetTemplateChild("ContentPresenter") as ContentPresenter;

        // This event can change the NotSelected state
        this.SelectionChanged += (s, e) => this.SetTextIfEmpty();

        // Set a state at start
        this.SetTextIfEmpty();
    }

    // This method can change the Empty state
    protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        base.OnItemsChanged(e);
        this.SetTextIfEmpty();
    }

    /// <summary>
    /// Text if the SelectedItem property is null.
    /// </summary>
    public string NotSelectedText
    {
        get { return (string)GetValue(NotSelectedTextProperty); }
        set { SetValue(NotSelectedTextProperty, value); }
    }

    public static readonly DependencyProperty NotSelectedTextProperty =
        DependencyProperty.Register("NotSelectedText", typeof(string), typeof(ExtendedComboBox), new PropertyMetadata(" "));

    /// <summary>
    /// Text if there are no items in the ComboBox at all.
    /// </summary>
    public string EmptyText
    {
        get { return (string)GetValue(EmptyTextProperty); }
        set { SetValue(EmptyTextProperty, value); }
    }

    public static readonly DependencyProperty EmptyTextProperty =
        DependencyProperty.Register("EmptyText", typeof(string), typeof(ExtendedComboBox), new PropertyMetadata(null));

    /// <summary>
    /// Changes the state of this control and updates the displayed text.
    /// </summary>
    protected void SetTextIfEmpty()
    {
        if (this.selectedContent == null || !(this.selectedContent.Content is TextBlock))
            return;
        var text = this.selectedContent.Content as TextBlock;

        if (this.SelectedItem == null && this.Items != null && this.Items.Count > 0)
        {
            text.Text = this.NotSelectedText;
            VisualStateManager.GoToState(this, ExtendedComboBox.StateNotSelected, true);
        }
        else if (this.SelectedItem == null)
        {
            text.Text = this.EmptyText ?? this.NotSelectedText;
            VisualStateManager.GoToState(this, ExtendedComboBox.StateEmpty, true);
        }
        else VisualStateManager.GoToState(this, ExtendedComboBox.StateNormal, true);
    }
}
vortexwolf
  • 13,967
  • 2
  • 54
  • 72
  • Is there an easy way to allow the user to select the "Not Selected" state after a selection has already been made? – Jordan May 20 '13 at 20:12
  • @Jordan Just set `.SelectedItem = null;` – vortexwolf May 21 '13 at 09:24
  • In code, I know. I'm talking about the user being able to set it back to "Not Selected". – Jordan May 21 '13 at 13:33
  • @Jordan Then you can use the built-in combobox control and add 1 item to the beginning of the items collection. – vortexwolf May 21 '13 at 13:42
  • That was what I was trying to avoid. You think this would be a common use case, but I guess its not. I am binding the combo box to objects not strings, so it makes things kind of messy. *sigh* Thanks, i really like your `ExtendedComboBox`, and I will definitely use it. – Jordan May 21 '13 at 13:56
0

You can use set value method to insert a value at a particular position. It may be not best option but it works for me.

var array= APPTasks.ListPhysician.OrderBy(e => e.phyFName).ThenBy(e => e.phyLName).ToArray();

array.SetValue((new Physician { phyFName = "Select Attening Md", phyLName = "" }), 0);

just bring your data in a variable and use SetValue method.

PRATEEK GHOSH
  • 243
  • 1
  • 15
0

Just do this:

theComboBox.SelectedItem  = yourDataItem;

alternatively, set the selected index:

theComboBox.SelectedIndex = 0;

Edit

If ItemSource is bound, you want to override the combo's DataContextChanged and then use one of the above lines to set the index/selected item.


However, if you don't want the default text to be selectable, you will have to do something along the lines of this.

Community
  • 1
  • 1
Chris Grant
  • 2,463
  • 23
  • 27