2011-05-30 105 views
2

XAML如何綁定TabControl?

<controls:TabControl x:Name="tabControlRoom" Grid.Row="1" Grid.Column="1" d:LayoutOverrides="Width, Height" ItemsSource="{Binding}" > 
      <controls:TabControl.ItemTemplate> 
       <DataTemplate> 
        <controls:TabItem Header="{Binding name}"> 
         <StackPanel Margin="10" Orientation="Horizontal"> 

         </StackPanel> 
        </controls:TabItem> 
       </DataTemplate> 
      </controls:TabControl.ItemTemplate> 
     </controls:TabControl> 

和代碼

m_roomContext.Load(m_roomContext.GetRoomQuery()); 
       tabControlRoom.DataContext = m_roomContext.Rooms; 

當我打開這個網頁,再有就是所有的元素,但第二個後,我只看到一個白色的屏幕

錯誤:

load operation failed for query 'GetRoom'. Unable to cast object of type 'Web.Room' to type 'System.Windows.Controls.TabItem'/'

+0

嗨,我已經有些問題,重新創建你的代碼與此參數d:LayoutOverrides =「寬度,高度」,反正你在哪裏放置標籤的負荷? – michele 2011-05-30 08:59:16

+0

michele,no。這不是問題。我更新帖子。 – Mediator 2011-05-30 09:13:03

+0

是什麼m_roomContext.Load()究竟做的,什麼是m_roomContext.Rooms的類型?我猜測,但我認爲錯誤在於背後的代碼,而不是綁定中的代碼。只要該選項卡的datacontext爲空,您可以看到所有元素,但只要演員失敗,就會空白。 – 2011-05-30 10:12:59

回答

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

當結合的TabControl有你需要完成兩件事情,一個是用於TabItem的其他報頭內容爲所選的TabItem,其通常是另一用戶控制的內容。

我過去解決這個問題的方法是將TabControl的ItemsSource綁定到一個視圖模型集合,並提供兩個數據模板,一個爲TabItem提供標題內容,另一個爲爲選定的TabItem提供映射到視圖的內容。

<Window.Resources> 
    <DataTemplate x:Key="ItemTemplate"> 
     <TextBlock Text="{Binding Title}" /> 
    </DataTemplate> 

    <DataTemplate x:Key="ContentTemplate"> 
     <local:SampleView /> 
    </DataTemplate> 
</Window.Resources> 

<Grid> 
    <TabControl 
     ItemsSource="{Binding SampleViewModels}" 
     ItemTemplate="{StaticResource ItemTemplate}" 
     ContentTemplate="{StaticResource ContentTemplate}" 
     SelectedIndex="0" 
     /> 
</Grid> 
相關問題