0
我有以下的TabControl:WPF - DataTemplate屬性裏面的元素沒有綁定時的問題?
<TabControl ItemsSource="{Binding Tabs"}>
<TabControl.ContentTemplate>
<DataTemplate DataType="{x:Type vm:TabVM}">
<TextBox></TextBox>
<TextBox Text="{Binding SomeProperty}"></TextBox>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
意外的行爲是第一個TextBox擁有所有的TabItems之間共享的Text屬性,而第二個文本框有效地結合視圖模型屬性。
我的需求是讓獨立的第一個文本框,即使沒有約束力。
我該怎麼辦?
**更新**
多次嘗試後,我決定使用ikriv的TabContent.cs。 我發現的唯一問題是,調用TabControl.Items.Refresh()(即刪除tabItem後)導致內部高速緩存的重置。
的unelegant而有效的解決方案可能是這樣的:
public ContentManager(TabControl tabControl, Decorator border)
{
_tabControl = tabControl;
_border = border;
_tabControl.SelectionChanged += (sender, args) => { UpdateSelectedTab(); };
/* CUSTOM */
var view = CollectionViewSource.GetDefaultView(((TabControl)_tabControl).Items);
view.CollectionChanged += View_CollectionChanged;
}
/*
* This fix the internal cache content when calling items->Refresh() method
* */
private void View_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
{
/* Retrieve all tabitems cache and store to a temp list */
IList<ContentControl> cachedContents = new List<ContentControl>();
foreach (var item in _tabControl.Items)
{
var tabItem = _tabControl.ItemContainerGenerator.ContainerFromItem(item);
var cachedContent = TabContent.GetInternalCachedContent(tabItem);
cachedContents.Add(cachedContent);
}
/* rebuild the view */
_tabControl.Items.Refresh();
/* Retrieve all cached content and store to the tabitems */
int idx = 0;
foreach (var item in _tabControl.Items)
{
var tabItem = _tabControl.ItemContainerGenerator.ContainerFromItem(item);
TabContent.SetInternalCachedContent(tabItem, cachedContents[idx++]);
}
}
}
嘗試'TabControl.ItemTemplate'代替'ContentTemplate' –
ItemTemplate是用於TabItem標題,而不是內容 –
如果你想設置項目模板,使用'TabControl.ItemContainerStyle'('