基本上我面臨的問題與Binding MenuItem's IsChecked to TabItem's IsSelected with dynamic tabs 012fol我自定義TabControl有自己的viewModel,我也有一個菜單綁定到同一個源。 發生了什麼事是綁定menuItem
的isChecked
到isSelected
不再工作。 I thought IsSelected can not be found as there's no such property in viewModel
綁定MenuItem的IsChecked到TabItem的isSelected
<Setter Property="IsChecked" Value="{Binding IsSelected, Mode=TwoWay}" />
我試圖使用該解決方案建議構建的TabItem
清單,但我得到的錯誤Unable to cast object of type TabData to type TabItem
。下面是我的xaml和轉換器。我認爲它失敗了,因爲在施工TabControl.items
將返回viewmodel實例,而不是UIControl TabItem
;任何建議如何在這裏做一個綁定?
XAML
<Menu Background="Transparent">
<MenuItem
Style="{StaticResource TabMenuButtonStyle}"
ItemsSource="{Binding RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type TabControl}},
Path=Items,Mode=OneWay,NotifyOnSourceUpdated=True,Converter={StaticResource TabControlItemConverter}}"
ItemContainerStyle="{StaticResource TabMenuItemxxx}">
</MenuItem>
</Menu>
C#
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ItemCollection ic = (ItemCollection)value;
List<TabItem> tabItems = new List<TabItem>();
foreach (var obj in ic)
{
tabItems.Add((TabItem)obj);
}
return tabItems;
}
Ty,非常漂亮的答案! 'CommandParameter =「{Binding}」''如何推斷'TabItem'屬於它自己? – baozi 2014-11-22 10:12:35
@baozi,'{Binding}'引用綁定到元素的對象,在這種情況下,它是TabData類的一個實例。請參閱[綁定標記擴展](http://msdn.microsoft.com/zh-cn/library/ms750413(v = vs.110).aspx)以獲取有關Unqualified {Binding}語法和[Binding Path Syntax](http綁定路徑語法) ://msdn.microsoft.com/en-us/library/ms752300(v = VS。110).aspx#Path_Syntax)獲取更多相同的細節。我很高興,該解決方案幫助您解決了一些問題。最後但並非最不重要的,快樂編碼:) – pushpraj 2014-11-22 13:04:18