开发者

WPF Combobox - PreSelect An Item

开发者 https://www.devze.com 2023-02-13 07:12 出处:网络
I want to pre-select a combobox, (select an existing item) from the itemssource. Here开发者_JAVA百科 is my class model and xaml syntax.

I want to pre-select a combobox, (select an existing item) from the itemssource. Here开发者_JAVA百科 is my class model and xaml syntax.

Class ViewModelSample
{
 Public List<Animal> Animals;
 Public LivingBeing  LivingBeingInst {get; set;}
}

Class LivingBeing
{
  Public Animal AnimalInst {get; set;} 
}

--------------------------------------------------------------------
<Combobox ItemsSource={Binding Animals} SelectedItem={Binding LivingBeingInst.AnimalInst}
 DisplayMemeber = SomePropertyInAnimal>
--------------------------------------------------------------------------

But this doesnt work. All I want is, when the xaml shows up, I need the combobox to be pre-selected with the item given in SelectedItem.

Any help is much appreciated. Thanks, Mani


Thanks for the input and I have resolved this. I had implemented INotifyPropertyChanged but that wasnt the issue.

The combobox's SelectedItem should be one of the values from the Collection that is given in ItemsSource. Though, AnimalInst is of type Animal, it is not one of the objects in te ItemsSource. So what I did is, declared a SelectedAnimal property, binded that to SelectedItem. 'SelectedAnimal' will return the same instance from 'Animals' collection comparing AnimalInst.Name. An example as in below. (Schedules is the collection).

 private ISchedule _selectedSchedule;
       public ISchedule SelectedSchedule
        {
            get
            {
                if(_selectedSchedule != null)
                {
                    var schedule = this.Schedules.Where(item => item.GlobalCodesId == _selectedSchedule.GlobalCodesId).FirstOrDefault();
                    return schedule;
                }
                return _selectedSchedule;
            }
           set 
           { 
               _selectedSchedule = value;
               base.NotifyPropertyChanged("SelectedSchedule");
           }
        }


Normally you'd have a List (in your case Animals) off of your ViewModel and a SelectedAnimal property, in your case, it appears that the LivingBeingInst property is supposed to be the selected animal from the list.

To get something like that to work, you'll probably have to write some code to convert a LivingBeing into an animal and back via ValueConverter.

However, I believe your best bet would be to create a SelectedAnimal property to store the selected Animal.

In your constructor, or wherever you initially populate the list of Animals, you can set the SelectedAnimal to the first element in the list.


The initial value of the SelectedItem needs to be an instance of an object that is a member of the ComboBox's ItemsSource. In this example, LivingBeingInst.AnimalInst would need to be in the Animals collection.

You could also have a property that finds the corresponding item to LivingBeingInst.AnimalInst in the Animals collection, like how the Everything Matters 's answer does.


You don't have change notification implemented in these classes. So unless you've populated all of these properties in the respective classes' constructors, there's no way for the bindings to know that you've done so.

At least, that's my guess from the non-working, non-compilable, not-really-code that you've posted. Generally speaking, you'll get more useful answers if you post real code, especially if you take the time to implement the smallest minimal subset of your existing code that still exhibits the problem you're trying to solve. (Among other things, you might find the answer yourself when you do that.)

0

精彩评论

暂无评论...
验证码 换一张
取 消