Binding SelecteItem in TabControl
It is MVVM project. I have a ListBox with TabControls inside of it's items. TabItems are different for each TabControl and are binded from ViewModel:
<TabControl ItemsSource="{Binding Tabs}"
</TabControl >
Tabs are:
public ObservableCollection<TabItem> Tabs
{ get { return tabs; }
set {
tabs = value;
value.CollectionChanged += delegate
{
OnPropertyChanged("Tabs");
};
OnPropertyChanged("Tabs"); } }
Tabs are filled by the following way only once:
Tabs = new ObservableCollection<TabItem>();
if (details.Image != null || !String.IsNullOrEmpty(details.Summary))
Tabs.Add(new TabItem() { Header = "General Info", Content = new GeneralInfo() { DataContext开发者_StackOverflow社区 = ItemDetails } }); ........... 6 different types
This is a bit info about what I have. Then I dinamically change DataContext's of this TabControls to simulate scrolling.
All info from ViewModel is binded into control, but there is a problem with TabControl. Headers are binded. but content doesnt. All headers are deselected and TabCntrol content area is empty.
I have tried to make TwoWayBinding on SelectedIndex and SelectedItem, but it doesnt help.... Seems SelectedIndex doesn't do anything that can select TabItem. And SelectedItem doesn't work,becuase as I think it has new reference after each changing of data context. Because TabControl.ItemsSource re-binds and new TabItems created.
Maybe someone solved same problem and knows solution?
Thanks
I have solved the my issue. The problem was that I used controls as DataContext: - List of TabItems - Controls inside TabItems content There was some processes, that I dont undrstand entire. But, idea is that while DataContext's changing, one DataContext can be assigned to 2 different TabControls. Since UI Controls cant belong to 2 parents at the same moment, so, binding that should show that controls from DataContext in content doesnt work. And doesnt throw excpetion (cause it's binding Smile | :) )
So, I remove al UI objects from DataContext, and add object models. And create UserControls that display are Views for these object models
精彩评论