绑定TabControl中的SelectedItem [英] Binding SelectedItem in TabControl
问题描述
嗨
这是MVVM项目.我有一个列表框,其中的TabControls位于其中.每个TabControl的TabItem都不相同,并从ViewModel绑定:
Hi
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 = ItemDetails } }); ........... 6 different types</tabitem>
这是关于我所拥有的一些信息.然后我会动态地更改此TabControls的DataContext,以模拟滚动.
来自ViewModel的所有信息都绑定到控件中,但是TabControl存在问题.标头已绑定.但内容没有.取消选择所有标头,并且TabCntrol内容区域为空.
我试图对SelectedIndex和SelectedItem进行TwoWayBinding,但这没有帮助....似乎SelectedIndex并没有做任何可以选择TabItem的事情.而SelectedItem不起作用,因为我认为在每次更改数据上下文后它都有新的引用.因为TabControl.ItemsSource重新绑定并且创建了新的TabItem.
也许有人解决了相同的问题并知道解决方法?
谢谢
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 tried to understand your problem and, I hope I guessed right.
如果您使用MVVM并更改了DataContext
,则您的视图模型可能包含不同的视图模型和更改DataContext
的代码.
If you work MVVM and you change DataContext
''s, you probably have a view-model that contains the different view-models and the code that changes the DataContext
''s.
将SelectedIndex
或SelectedItem
绑定到当前视图模型无济于事,因为在更改DataContext
之后,原始视图模型(绑定到的视图模型)已经消失了
Binding SelectedIndex
or SelectedItem
to the current view-model doesn''t help since after the DataContext
had been changed the original view-model (the view-model that you was bound to) has already gone.
尝试向视图模型添加另一个属性,该属性包含更改DataContext
的代码.此属性可以容纳TabControl
的SelectedIndex
,并且可以在更改DataContext
's时使用.
Try to add another property to the view-model that hold the code that changes the DataContext
''s. This property can hold the SelectedIndex
for the TabControl
and, can be used when you change the DataContext
''s.
我已经解决了我的问题.
问题是我将控件用作DataContext:
-TabItem列表
-TabItems内容内的控件
有一些过程,我并没有完全理解.但是,想法是,在更改DataContext的同时,可以将一个DataContext分配给2个不同的TabControl.由于UI控件不能同时属于2个父级,因此,应该显示内容中来自DataContext的控件的绑定无效.并且不抛出异常(因为它是绑定的:))
因此,我从DataContext中删除了所有UI对象,并添加了对象模型.并创建显示为这些对象模型的视图的用户控件
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 :) )
So, I remove al UI objects from DataContext, and add object models. And create UserControls that display are Views for these object models
这篇关于绑定TabControl中的SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!