2012-10-26 29 views
0

的屬性我使用其ItemSource屬性將集合綁定到TabControl。將選項卡標題綁定到代碼

我正在編寫代碼而不是XAML中的WPF以獲得更深入的理解。

我面對的問題是,如果我想一個的TabItem的標題綁定到一個屬性(「ENTITYID」)綁定不一命嗚呼

代碼工作,如果我設置的值代替結合(以下代碼中的註釋)

var binding = new Binding(); 
binding.Path = new PropertyPath("EntityID"); 

DataTemplate itemTemplate = new DataTemplate(); 
var label = new FrameworkElementFactory(typeof(Label)); 

//label.SetValue(Label.ContentProperty,"test"); 
label.SetBinding(Label.ContentProperty, binding); 

itemTemplate.VisualTree = label; 

_tabControl.ItemTemplate = itemTemplate; 

此外,如果設置的ContentTemplate代替ItemTemplate中的結合作品爲好。

如何從純代碼將標籤頁眉綁定到我的ItemsSource的屬性?

回答

1

有許多方法可以從Code Behind設置綁定。你應該嘗試綁定的是TabItem上的HeaderProperty。儘管你必須先檢索它才能做到這一點。

下面是一個應該設置你開始的工作示例。這不是我會這麼做的方式,因爲我會在xaml中這樣做,但是正如你所要求的那樣,通過代碼來做到這一點,在這裏你去:)

在附註中,定義幾乎總是一個壞主意在Code後面的模板,儘量避免它。

Windows.xaml

<Window x:Class="StackOverflow.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    xmlns:local="clr-namespace:StackOverflow" 
    Title="Super long title of the super window" Width="500" Height="300"> 

    <Window.Resources> 
     <DataTemplate DataType="{x:Type local:Entity}"> 
      <TextBlock Text="{Binding Id}" FontSize="{Binding Size}" /> 
     </DataTemplate> 
    </Window.Resources> 

    <local:MyTabControl x:Name="tabControl" ItemsSource="{Binding Entities}" /> 

</Window> 

Window.xaml.cs

using System.Windows; 
using System; 
using System.Windows.Data; 
using System.Collections.Generic; 
using System.Windows.Controls; 

namespace StackOverflow 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 

    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = this; 
    } 

    public IEnumerable<Entity> Entities 
    { 
     get 
     { 
      for (int i = 0; i < 5; i++) 
      { 
       yield return new Entity() { Id = i }; 
      } 
     } 
    } 
} 

public class MyTabControl : TabControl 
{ 
    protected override DependencyObject GetContainerForItemOverride() 
    { 
     var tabitem = base.GetContainerForItemOverride() as TabItem; 
     if (tabitem != null) 
     { 
      tabitem.Loaded += OnTabItemLoaded; 
     } 
     return tabitem; 
    } 

    void OnTabItemLoaded(object sender, RoutedEventArgs e) 
    { 
     var tabItem = sender as TabItem; 
     if (tabItem == null) 
      throw new InvalidOperationException(); 
     tabItem.SetBinding(TabItem.HeaderProperty, new Binding("DisplayName")); 
    } 
} 

public class Entity : DependencyObject 
{ 
    public int Id { get; set; } 
    public string DisplayName { get { return "Entity ID : " + Id; } } 
} 
} 
1

夫婦的事情...

作爲一個WPF設計和開發人員,XAML是GUI和代碼 Beh的最佳方式ind隔離。它不會以任何方式減少我對中對WPF的理解。所以我推薦XAML。

相信我是一個WinForms/ASP.NET開發我自己,我仍然不願意使用XAML的,但我不得不寫的代碼泰坦尼克量和各種GUI元素之間並在該馴服的關係野獸叫做Templates \ Styles \ TriggersResourceDictionaries僅僅使用C#代碼背後對我來說只是一個普通的折磨。

足夠我的經驗,這是關於你的問題!

要回答你的問題,你有沒有設置_tabControl.ItemsSource?並確保ItemsSource中的每個項目都具有EntityID屬性。你的代碼應該工作。

如果這仍然無法正常工作,請嘗試在Visual Studio的Output窗口中查看是否存在任何綁定錯誤。

相關問題