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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:28:22+00:00 2026-06-03T05:28:22+00:00

I have a class ParamObj (which implements INotifyPropertyChanged ) which contains a property DefaultValues

  • 0

I have a class ParamObj (which implements INotifyPropertyChanged) which contains a property DefaultValues of type ObservableCollection<Param>. My Param class (which also implements INotifyPropertyChanged) contains two properties: Name and IsSelected.

On button click I am populating a StackPanel with TextBlock and ComboBox controls. Each Param in the collection DefaultValues is an option in the ComboBox, where the Name property represents the text of that option and IsSelected is a boolean value representing if that option is the one currently selected in the ComboBox.

Param class below:

public class Param : INotifyPropertyChanged
{
    public enum ParamType
    {
        Unknown=-1,
        Text=0,
        Option=1,
        Multi=2
    }

    private string pName = string.Empty;
    private bool pSelected = false;
    private ParamType pType = ParamType.Unknown;

    public string Name { get { return pName; } set { pName = value; OnPropertyChanged("Name"); } }
    public bool IsSelected { get { return pSelected; } set { pSelected = value; OnPropertyChanged("IsSelected"); } }
    public ParamType Type { get { return pType; } }

    public Param(string name, bool selected)
    {
        this.pName = name;
        this.pSelected = selected;
        this.pType = ParamType.Option;
    }

    //INotifyPropertyChanged base
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(name));
    }
}

ParamObj class below:

public class ParamObj : INotifyPropertyChanged
{
    //ui type mapping
    public enum UITypeEnum
    {
        Textbox = 0,
        ComboBox = 1,
        CheckableComboBox = 2
    }

    private string name = string.Empty;
    private UITypeEnum uiType = UITypeEnum.Textbox;
    private ObservableCollection<Param> defaultVals = new ObservableCollection<Param>();
    private string propDescription = string.Empty;

    public string Name { get { return name; } set { name = value; OnPropertyChanged("Name"); } }
    public UITypeEnum UIType { get { return uiType; } set { uiType = value; OnPropertyChanged("UIType"); } }
    public ObservableCollection<Param> DefaultValues { get { return defaultVals; } }
    public string Desc { get { return propDescription; } set { propDescription = value; } }

    //Initialization
    public ParamObj()
    {

    }

    public void ClearDefaultValues()
    {
        this.defaultVals.Clear();
        OnPropertyChanged("DefaultValues");
    }

    public void AddDefaultValue(Param objAdded)
    {
        this.defaultVals.Add(objAdded);
    }

    public void AddDefaultValueRange(Param[] objsToAdd)
    {
        foreach (Param o in objsToAdd)
        {
            this.defaultVals.Add(o);
        }
    }

    //INotifyPropertyChanged base
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(name));
    }
}

Code below is called in the button click event. po represents a ParamObj instance.

case ParamObj.UITypeEnum.ComboBox:
    cb = new ComboBox();
    cb.Height = 26;
    cb.Width = 210;
    cb.Margin = new Thickness(2);
    cb.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
    cb.DataContext = po.DefaultValues;
    //binding
    cb.ItemsSource = po.DefaultValues;
    cb.DisplayMemberPath = "Name";                        
    cb.SelectedValuePath = "IsSelected";
    cb.SelectedValue = true;

    cb.SelectionChanged += ParamComboBox_SelectionChanged;
    break;

Here’s the problem. ComboBox items are populated successfully and the option (Param object) that has an IsSelected value of true is selected. What I thought would happen is that when I changed the selected option in the ComboBox the IsSelected property’s value (boolean) of the Param object would be toggled from false to true. This didn’t happen. So I added the cb.SelectionChanged += ParamComboBox_SelectionChanged event to the ComboBox.

    private void ParamComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (!(sender is ComboBox)) return;

        ComboBox cb = sender as ComboBox;
        Param cp = (Param)cb.SelectedItem;
        if (cp.IsSelected == false)
        {
            foreach (Param p in cb.ItemsSource)
            {
                p.IsSelected = false;
            }
            cp.IsSelected = true;
        }
    }

Through debugging I do see that the IsSelected properties are correctly updated, but the values in my po object are not updated with the new values.

Any suggestions are greatly appreciated. I’ve tried my darndest to figure this out but I’m stuck.

Brian

  • 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-03T05:28:23+00:00Added an answer on June 3, 2026 at 5:28 am

    This is one of the problems that appear to be very simple, but seems like there is no easy or direct way to bind the ComboBoxItem IsSelected to a given property. One possible solution that comes to my mind could be:

    First, Add a Style of type ComboBoxItem to your ComboBox and add a Trigger to that style. This trigger will fire when you have selected a ComboBoxItem and will call the helper class in step 2.

         <ComboBox Name="TestCombo" ItemsSource="{Binding Path=DataSource.DefaultValues}" DisplayMemberPath="Name" SelectedValuePath="IsSelected" SelectionChanged="Test_SelectionChanged">
                    <ComboBox.Resources>                    
                        <Style TargetType="{x:Type ComboBoxItem}">
                            <Style.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="StackOverflow:IsSelectedBehavior.IsSelected" Value="True"></Setter>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="False">
                                    <Setter Property="StackOverflow:IsSelectedBehavior.IsSelected" Value="False"></Setter>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ComboBox.Resources>
                </ComboBox>
    

    If you want to do this same thing programmatically, then you go:

            Style style = new Style();
            Trigger t = new Trigger();
            style.TargetType = typeof(ComboBoxItem);
            t.Property = ListBoxItem.IsSelectedProperty;
            t.Value = true;
            Setter setter = new Setter(IsSelectedBehavior.IsSelectedProperty, true);
            t.Setters.Add(setter);
            style.Triggers.Add(t);
            cb.Resources.Add(typeof(ComboBoxItem), style);
    

    Then, Create a IsSelectedBehaviorClass to handle your IsSelected Behavior. This static class will receive the currently selected ComboBoxItem and set the Param.IsSelected property accordingly. Something like this:

        public static class IsSelectedBehavior
        {
            public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.RegisterAttached(
                "IsSelected", typeof (bool), typeof (IsSelectedBehavior), new UIPropertyMetadata(false, OnIsSelected));
    
            public static bool GetIsSelected(DependencyObject d)
            {
                return (bool) d.GetValue(IsSelectedProperty);
            }
    
            public static void SetIsSelected(DependencyObject d, bool value)
            {
                d.SetValue(IsSelectedProperty, value);
            }
    
            public static void OnIsSelected(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                //Get current ComboBoxItem
                ComboBoxItem test = d as ComboBoxItem;
                //Get the current Param from the ComboBoxItem.Content
                Param p = test.Content as Param;
                //Set the Param.IsSelected property
                p.IsSelected = (bool) e.NewValue;
            }
        }
    

    Finally you can test your solution on the SelectionChanged EventHandler

        private void ParamComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                Param selected = cb.SelectedItem as Param;
                if (selected != null)
                {
                    Debug.WriteLine("Selection Changed: Selected " + selected.Name);
                }
                StringBuilder str = new StringBuilder();
                foreach (var obj in cb.Items)
                {
                    Param p = obj as Param;
                    str.Append("Name:" + p.Name + " IsSelected:" + p.IsSelected + Environment.NewLine);
                    Debug.WriteLine(str);
                }
                MessageBox.Show(str.ToString());
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class class DateOptTimeType implements org.hibernate.usertype.UserType that works with two columns @org.hibernate.annotations.Type(type =
I have class which implements Countable, ArrayAccess, Iterator and Serializable. I have a public
I have class, that contains different types and implements Iterable<Object> interface. But it is
I have class(Customer) which holds more than 200 string variables as property. I'm using
I have class A that has some primitive attributes and also member of type
I have class deriving from TextBox to which I attach a dependency property of
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have class A, which exposes an event. an object of class B subscribed
I have class User that contains protected constructor and class Account that has access

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.