2010-09-02 110 views
5

我在ViewModel中有模型對象的集合。我希望能夠將TabControl綁定到這些對象,並使用DataTemplate從模型對象中提取信息。當我嘗試這樣做時,我得到了錯誤消息:無法將類型爲Model的對象轉換爲類型爲TabItem的對象。花一些時間尋找一個解決方案後,我發現以下幾點:將Silverlight TabControl綁定到集合

  1. Silverlight的TabControl的是 壞了。使用ListBox 和ContentControl的組合來模擬TabControl的 行爲。 (手段 ,我要皮膚列表框到 看起來像一個TabControl)

  2. 的TabControl不覆蓋 PrepareContainerForItemOverride和 的解決方案是讓一個 轉換器。 (不太好,因爲我 則需要指定在轉換器的 convertee的類型)

任何人都知道任何更好的解決方案?

XAML

<sdk:TabControl ItemsSource="{Binding Items, ElementName=MyControl}"> 
     <sdk:TabControl.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Name}" /> 
      </DataTemplate> 
     </sdk:TabControl.ItemTemplate> 
    </sdk:TabControl> 

C#

public ObservableCollection<Model> Items { get; set; } 

public ViewModel() 

    Items = new ObservableCollection<Model>{ 
     new Model { Name = "1"}, 
     new Model { Name = "2"}, 
     new Model { Name = "3"}, 
     new Model { Name = "4"} 
    }; 
} 

Suggested Converter

public class TabConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     List<TabSource> source = value as List<TabSource>; 
     if (source != null) 
     { 
      List<TabItem> result = new List<TabItem>(); 
      foreach (TabSource tab in source) 
      { 
       result.Add(new TabItem() 
       { 
        Header = tab.Header, 
        Content = tab.Content 
       }); 
      } 
      return result; 
     } 
     return null; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

我已經創建,並不需要一個轉換器和擴展選項卡控制的博客採取的正常工作與ObservableCollection類。 http://vortexwolf.wordpress.com/2011/04/09/silverlight-tabcontrol-with-data-binding/ – vorrtex 2011-10-29 23:42:45

回答

2

創建轉換

public class SourceToTabItemsConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      try 
      { 
       var source = (IEnumerable)value; 
       if (source != null) 
       { 
        var controlTemplate = (ControlTemplate)parameter; 

        var tabItems = new List<TabItem>(); 

        foreach (object item in source) 
        { 
         PropertyInfo[] propertyInfos = item.GetType().GetProperties(); 

         //тут мы выбираем, то поле которое будет Header. Вы должны сами вводить это значение. 
         var propertyInfo = propertyInfos.First(x => x.Name == "name"); 

         string headerText = null; 
         if (propertyInfo != null) 
         { 
          object propValue = propertyInfo.GetValue(item, null); 
          headerText = (propValue ?? string.Empty).ToString(); 
         } 

         var tabItem = new TabItem 
              { 
               DataContext = item, 
               Header = headerText, 
               Content = 
                controlTemplate == null 
                 ? item 
                 : new ContentControl { Template = controlTemplate } 
              }; 

         tabItems.Add(tabItem); 
        } 

        return tabItems; 
       } 
       return null; 
      } 
      catch (Exception) 
      { 
       return null; 
      } 
     } 

     /// <summary> 
     /// ConvertBack method is not supported 
     /// </summary> 
     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotSupportedException("ConvertBack method is not supported"); 
     } 

創建控件模板:

<ControlTemplate x:Key="MyTabItemContentTemplate"> 
      <StackPanel> 
       <TextBlock Text="{Binding Path=name}" /> 
      </StackPanel> 
     </ControlTemplate> 

和有約束力的轉換,控件模板

<controls:TabControl x:Name="tabControl" 
     ItemsSource="{Binding ElementName=tabControl, 
           Path=DataContext, 
           Converter={StaticResource ConverterCollectionToTabItems}, 
           ConverterParameter={StaticResource MyTabItemContentTemplate}}"> 
     </controls:TabControl> 

binding-tabcontrol

+1

請用英文總結您的博文內容。此外,當您鏈接到您的博客或任何您所屬的網站,產品或項目時,您需要披露這是您自己的工作。請參閱常見問題解答中的[我可以在此推廣產品或網站?](http://stackoverflow.com/faq#promotion)部分。 – 2011-06-03 11:27:02

相關問題