2014-11-14 105 views
1

基本上我面臨的問題與Binding MenuItem's IsChecked to TabItem's IsSelected with dynamic tabs 012fol我自定義TabControl有自己的viewModel,我也有一個菜單綁定到同一個源。 發生了什麼事是綁定menuItemisCheckedisSelected不再工作。 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; 
} 

enter image description here

回答

2

這裏所提供的項目的變化基地

刪除從綁定以下,不要求

,Mode=OneWay,NotifyOnSourceUpdated=True,Converter={StaticResource TabControlItemConverter} 

修改二傳手在風格TabMenuItemxxx

<Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=TabItem}}" /> 

<Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay/> 

添加以下二傳手的風格TargetType="{x:Type TabItem}"

<Setter Property="IsSelected" Value="{Binding IsSelected}" /> 

修改TabData類如下

public class TabData : INotifyPropertyChanged 
{ 
    private bool isselected; 
    public string Header { get; set; } 
    public object Content { get; set; } 
    public bool IsEnabled { get; set; } 
    public bool IsSelected 
    { 
     get { return isselected; } 
     set 
     { 
      if (ViewModel.CurrentItem.IsSelected && ViewModel.CurrentItem != this) 
      { 
       ViewModel.CurrentItem.IsSelected = false; 
      } 
      isselected = value; 
      RaisePropertyChanged("IsSelected"); 

      if (ViewModel.CurrentItem != this) 
       ViewModel.CurrentItem = this; 

     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 

    public void RaisePropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

這是所有你需要在你的項目中同步菜單項的更改進行檢查,以片項目的選擇。

你對關閉標籤項目第二個問題,你可以通過改變關閉按鈕的

CommandParameter="{Binding SelectedItem,ElementName=tabControl}" 

CommandParameter="{Binding}" 

樣本項目修復TabControlSyncWithMenuItems.zip

讓我知道結果。

+0

Ty,非常漂亮的答案! 'CommandParameter =「{Binding}」''如何推斷'TabItem'屬於它自己? – baozi 2014-11-22 10:12:35

+0

@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

相關問題