I have this throuble:
There observable collection, which bind to combobox. When collection contains any items, all works fine, I add items in collection, then choose him in combobox, selected value is no开发者_运维问答t null.
I have items control,which contains any component controls which reflect any entity. I may select parameter this entity in combobox which bind to observable collection.
If I add first new entity in content control and collection is empty, then add item in collection - this item automaticaly select in combobox, but combobox selected value is null. It turns out, this situation: item select in combobox, but combobox selected value is null. If i select this item manualy, selected value is set to this item and work further аштую I don't understand how it may be. There is my code: this is collection
public ObservableCollection<AnyType> AnyTemplates { get; set; }
this is xaml code combobox:
<ComboBox Name="cmbKeyA"
Width="100"
SelectedValue="{Binding Path=KeyAName}"
ItemsSource="{Binding Path=DataContext.KeyTemplates, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
DisplayMemberPath="Name"
SelectedValuePath="Name"/>
I am not quite sure I understand your problem, but from what I do understand I would use either a Dependency Property or a Property that implements the INotifyPropertyChanged event to bind to SelectedItem of the ComboBox. Then when I wanted to add an Item to the ObservableCollection (assuming you want it to be selected), I would both add it to the collection and set my new Property equal to it.
精彩评论