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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:38:49+00:00 2026-05-24T15:38:49+00:00

I have template for my ComboBox . taken from here . Lets say each

  • 0

I have template for my ComboBox. taken from here. Lets say each item is Node type.

Its all work perfect, but one thing crash my mind. When I click between ComboBoxItem (this area i think one-pixel line), Text of ComboBox changed to TypeName of items (with namespace).

I have overrided ToString() method of Node class, but CallStack shows, that program comes to it from [External code].

What should I do to ComboBox.Text show my string property instead of TypeName of ComboBoxItem?

If I miss some detail, just point out what is.

Edit:

Code is from hyperlink here above, with some changes.

Usage:

<vm:ComboCheckBox Grid.Column="1" ItemsSource="{Binding Path=Months}"
                  DefaultText="Select months"/>

Months is ObservableNodeCollection.

Template[I don’t place here resources. they are the same, as at link]:

<ComboBox   ItemsSource="{Binding ElementName=UserControl, Path=ItemsSource}"
            DataContext="{Binding ElementName=UserControl, Path=DataContext}"
            Text="{Binding Path=Text, Mode=OneWay, ElementName=UserControl, UpdateSourceTrigger=PropertyChanged}"
            Focusable="False"
            IsEditable="False"
            Loaded="CheckableCombo_Loaded"

            x:Name="CheckableCombo"
            SnapsToDevicePixels="True"
            OverridesDefaultStyle="True"
            ScrollViewer.HorizontalScrollBarVisibility="Auto"
            ScrollViewer.VerticalScrollBarVisibility="Auto"
            ScrollViewer.CanContentScroll="True"
            IsSynchronizedWithCurrentItem="True"
            MinWidth="120"
            MinHeight="20"
            >
    <ComboBox.ItemTemplate>
        <HierarchicalDataTemplate>
                <CheckBox Margin="0" IsChecked="{Binding Path=IsSelected}"
                          Command="{Binding Path=CheckBoxStateChanged, ElementName=UserControl}" CommandParameter="{Binding}"
                          Content="{Binding Path=Title}" />
        </HierarchicalDataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBox.Template>
        <ControlTemplate TargetType="ComboBox">
            <Grid>
                <ToggleButton 
                                    Name="ToggleButton" 
                                    Template="{StaticResource ComboBoxToggleButton}" 
                                    Grid.Column="2" 
                                    Focusable="false"
                                    IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                                    ClickMode="Press">
                </ToggleButton>
                <ContentPresenter
                                    x:Name="Presenter"
                                    IsHitTestVisible="False" 
                                    Margin="3,3,23,3"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Left">
                    <ContentPresenter.Content>
                        <TextBlock TextTrimming="CharacterEllipsis"
                                            Text="{Binding Path=Text,Mode=OneWay,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}}" />
                    </ContentPresenter.Content>
                </ContentPresenter>
                <TextBox x:Name="EditableTextBox"
                         Style="{x:Null}" 
                         Template="{StaticResource ComboBoxTextBox}" 
                         HorizontalAlignment="Left" 
                         VerticalAlignment="Center" 
                         Margin="3,3,23,3"
                         Focusable="True"
                         Background="Transparent"
                         Visibility="Hidden"
                         IsReadOnly="{TemplateBinding IsReadOnly}"/>
                <Popup Name="Popup"
                       Placement="Bottom"
                       IsOpen="{TemplateBinding IsDropDownOpen}"
                       AllowsTransparency="True" 
                       Focusable="False"
                       PopupAnimation="Slide">
                    <Grid Name="DropDown"
                          ShowGridLines="True"
                          SnapsToDevicePixels="True"                
                          MinWidth="{TemplateBinding ActualWidth}"
                          MaxHeight="{TemplateBinding MaxDropDownHeight}">
                        <Border Focusable="False"
                                x:Name="DropDownBorder"
                                Background="{StaticResource WindowBackgroundBrush}"
                                BorderThickness="1"
                                BorderBrush="{StaticResource SolidBorderBrush}"/>
                        <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" DataContext="{Binding}">
                            <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" KeyboardNavigation.ControlTabNavigation="Cycle" />
                        </ScrollViewer>
                    </Grid>
                </Popup>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="HasItems" Value="false">
                    <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                </Trigger>
                <Trigger Property="IsGrouping" Value="true">
                    <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                </Trigger>
                <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                    <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
                    <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
                </Trigger>
                <Trigger Property="IsEditable" Value="true">
                    <Setter Property="IsTabStop" Value="false"/>
                    <Setter TargetName="EditableTextBox" Property="Visibility" Value="Visible"/>
                    <Setter TargetName="Presenter" Property="Visibility" Value="Hidden"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ComboBox.Template>
</ComboBox>

CodeBehind:

/// <summary>
/// Interaction logic for ComboCheckBox.xaml
/// </summary>
public partial class ComboCheckBox : UserControl
{
    public ObservableNodeCollection ItemsSource 
    {
        get 
        { 
            return (ObservableNodeCollection)GetValue(ItemsSourceProperty); 
        } 
        set 
        {
            SetValue(ItemsSourceProperty, value);
            SetText();
        }
    }

    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(ObservableNodeCollection), typeof(ComboCheckBox), new UIPropertyMetadata(null)); 

    /// <summary> 
    /// Gets or sets the text displayed in the ComboBox 
    /// </summary> 
    public string Text 
    { 
        get
        { 
            return(string)GetValue(TextProperty); 
        }
        set 
        { 
            SetValue(TextProperty, value);
        } 
    } 

    public static readonly DependencyProperty TextProperty = 
        DependencyProperty.Register("Text", typeof(string), typeof(ComboCheckBox), new UIPropertyMetadata(string.Empty)); 


    /// <summary> 
    /// Gets or sets the text displayed in the ComboBox if there are no selected items 
    /// </summary> 
    public string DefaultText 
    { 
        get
        {
            return(string)GetValue(DefaultTextProperty);
        }
        set
        {
            SetValue(DefaultTextProperty, value);
            SetText();
        }
    }

    public static readonly DependencyProperty DefaultTextProperty = 
        DependencyProperty.Register("DefaultText", typeof(string), typeof(ComboCheckBox), new UIPropertyMetadata(string.Empty)); 

    public ComboCheckBox()
    {
        InitializeComponent();
        this.SetText();
    }

    public ICommand CheckBoxStateChanged
    {
        get
        {
            return new Helpers.DelegateCommand<Node>((Node parameter) =>
            {
                if (parameter.IsSelectAllNode)
                {
                    bool? isSelected = this.ItemsSource[0].IsSelected;
                    ItemsSource.ToList().ForEach(item => item.IsSelected = isSelected);
                }
                else
                {
                    if (this.ItemsSource[0].IsSelectAllNode)
                    {
                        bool isAllTrue = true, isAllFalse = true;
                        for (int i = 1; i < this.ItemsSource.Count; i++)
                        {
                            isAllTrue = isAllTrue && this.ItemsSource[i].IsSelected.Value;
                            isAllFalse = isAllFalse && !this.ItemsSource[i].IsSelected.Value;
                        }
                        if (isAllTrue)
                            this.ItemsSource[0].IsSelected = true;
                        else if (isAllFalse)
                            this.ItemsSource[0].IsSelected = false;
                        else
                            this.ItemsSource[0].IsSelected = null;
                    }
                }
                this.SetText();
            });
        }
    }

    private void SetText()
    {
        string answer = String.Empty;
        if (ItemsSource != null)
            answer = ItemsSource.ToString();
        if (String.IsNullOrWhiteSpace(answer))
            answer = this.DefaultText;
        this.Text = answer;
    }

    private void CheckableCombo_Loaded(object sender, RoutedEventArgs e)
    {
        this.SetText();
    }
}

Node class:

public class Node : ObservableObject
{
    public Node(string title, string header = "",  bool isSelectAllNode = false, bool? isSelected = false)
    {
        this.Title = title;
        if (String.IsNullOrEmpty(header))
            this.Header = title;
        else
            this.Header = header;
        this.IsSelectAllNode = isSelectAllNode;
        this.IsSelected = isSelected;
    }
    public string Header { get; set; }
    public string Title { get; set; }
    private bool? isSelected;
    private const string IsSelectedPropertyName = "IsSelected";
    public bool? IsSelected
    {
        get
        {
            return this.isSelected;
        }
        set
        {
            this.isSelected = value;
            this.OnPropertyChanged(IsSelectedPropertyName);
        }
    }
    public bool IsSelectAllNode { get; set; }
    public override string ToString()
    {
        return "123";//if miss it, the typeName will return.
    }
}

ObservableNodeCollection:

public class ObservableNodeCollection : ObservableCollection<Node>
{
    public ObservableNodeCollection()
    {
    }
    public ObservableNodeCollection(List<Node> list)
        : base(list)
    {
    }
    public ObservableNodeCollection(IEnumerable<Node> collection)
        : base(collection)
    {
    }

    public override string ToString()
    {
        string answer = String.Empty;
        if (this.Items != null)
        {
            StringBuilder outString = new StringBuilder();
            foreach (Node node in this.Items)
                if (node.IsSelected == true && !node.IsSelectAllNode)
                {
                    outString.Append(node.Header);
                    outString.Append(", ");
                }
            answer = outString.ToString().TrimEnd(new char[] { ',', ' ' });
        }
        return answer;
    }
}

EDIT 2:

Inside of the ViewModel constructor of the main window exist this code

        int i;
        //Initialize months for combobox
        this.Months = new ObservableNodeCollection(new List<Node>() { new Node("SelectAll", isSelectAllNode:true)});

        for (i=0; i < DateTimeFormatInfo.CurrentInfo.MonthNames.Length; i++)
        {
            if (!String.IsNullOrEmpty(DateTimeFormatInfo.CurrentInfo.MonthNames[i]))
                this.Months.Add(
                    new Node
                        (   
                            DateTimeFormatInfo.CurrentInfo.MonthNames[i], 
                            DateTimeFormatInfo.CurrentInfo.AbbreviatedMonthNames[i]
                        ));
        }

so, this.Months – is a ObservableNodeCollection, which have 1-st element is SelectAll, and other 12 elements is a months.

  • 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-24T15:38:49+00:00Added an answer on May 24, 2026 at 3:38 pm

    So you’re saying that if you remove the Node.ToString() method it displays the TypeName of the Node?

    That is the expected behavior. Your ComboBox items are bound to each item in your ObservableCollection, and when binding an object to a Text property, the ToString method is always used.

    You can set the ComboBox.Text property manually, however it is changed to SelectedItem.ToString() whenever the selected ComboBox item changes. By default this resolves to the TypeName of the ComboBox’s data item, however you can change this by overriding the ToString() method of the data item, or by setting the ComboBox.DisplayMemberPath to a property that exists on the data item

    <ComboBox ItemsSource="{Binding Months}"
              DisplayMemberPath="Header" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, lets say I have a ComboBox with a custom data template. One of
I have 10 ComboBox controls that will use the same item template (an Image
I have a WPF toolkit DataGrid as the dropdown in a ComboBox template. <toolkit:DataGrid
I have a ComboBox in WPF which is databound, and has data template which
I have a simple template for a combobox structured in this way: <ComboBox DockPanel.Dock=Left
I have a business application (created from template) and I can change language dynamically
I have a control derived from ComboBox , I want to use the ComboBox
I have a ComboBox with fairly complex template for individual items, which includes two
I have a DataGrid (WPF Toolkit) with a custom combobox like edit template of
I have a ComboBox. Without changing the template, is there a way that I

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.