2010-09-02 20 views
5

ViewModel cihazımda Model nesneleri koleksiyonu var. Bunlara bir TabControl bağlayabilmeyi ve Model Nesnelerinden bilgiyi ayıklamak için bir DataTemplate kullanabilmeyi isterim. Bunu yapmaya çalıştığımda, errormessage'ı alırım: Type Type nesnesine Object [Nesne] nesnesine nesne dönüştürülemedi. Koleksiyona bir Silverlight TabControl bağlama

  1. Silverlight TabControl

    kırık geçerli: çözüm arama biraz zaman geçirdikten sonra aşağıdaki buldum. Bir TabControl'ün davranışını taklit etmek için bir ListBox ve ContentControl bileşimini kullanın. (Means Ben bir TabControl gibi görünmek için ListBox derisini zorunda)

  2. TabControl PrepareContainerForItemOverride ve çözümü Converter yapmaktır geçersiz kılmaz. (Ben sonra Converter'da convertee türünü belirtmek gerekir, çünkü bu kadar iyi değil)

Herkes daha iyi bir çözüm biliyor?

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

blogdan alınan

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

düzgün çalışır

ObservableCollection sınıfı ile. http://vortexwolf.wordpress.com/2011/04/09/silverlight-tabcontrol-with-data-binding/ – vorrtex

cevap

2

oluştur dönüştürücü

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"); 
     } 

oluştur ControlTemplate:

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

Ve bağlayıcı dönüştürmek, ControlTemplate bir dönüştürücü gerekir ve yok genişletilmiş sekme denetimi oluşturduk binding-tabcontrol

+1

Lütfen blog yayınınızın içeriğini İngilizce olarak buradan özetleyin. Ayrıca, blogunuza veya bağlı olduğunuz herhangi bir web sitesine, ürüne veya projeye bağlandığınızda kendi işinizin olduğunu açıklamanız gerekir. SSS’in [http://stackoverflow.com/faq#promotion] bölümüne bakınız. –