2012-04-02 8 views
0

私はC#コードでTabControlを作成します。 ItemsSourceをコレクションにバインドし、マージンを設定します。 なんらかの理由で、DisplayMemberPathを設定することはできません。CのTabControlのDisplayMemberPathを設定します

_tabControl = new TabControl(); 
_tabControl.Margin = new Thickness(5); 
_tabControl.DisplayMemberPath = "Header"; 
_tabControl.SetBinding(ItemsControl.ItemsSourceProperty, itemsSourceBinding); 

各アイテムには「ヘッダー」というプロパティがあります。

なぜ機能しないのですか?

アンドレ

EDIT: ここでは、関連するすべてのコードです:

public partial class VariationGroupPreviewOptionsView 
{ 
    public string Header { get; set; } 

    public VariationGroupPreviewOptionsView() 
    { 
     InitializeComponent(); 
     DataContext = new VariationGroupPreviewOptionsViewModel(); 
    } 
} 

private void OptionsCommandExecute() 
{ 
    var dlg = new OptionsDialog(); 
    dlg.ItemsSource = new List<ContentControl>() {new VariationGroupPreviewOptionsView(){Header = "Test"}}; 
    dlg.ShowDialog(); 
} 

public class OptionsDialog : Dialog 
{ 

    public static readonly DependencyProperty ItemsSourceProperty = 
     DependencyProperty.Register("ItemsSource", typeof (IEnumerable), typeof (OptionsDialog), new PropertyMetadata(default(IEnumerable))); 

    public IEnumerable ItemsSource 
    { 
     get { return (IEnumerable) GetValue(ItemsSourceProperty); } 
     set { SetValue(ItemsSourceProperty, value); } 
    } 

    private readonly TabControl _tabControl; 


    public OptionsDialog() 
    { 
     DataContext = this; 
     var itemsSourceBinding = new Binding(); 
     itemsSourceBinding.Path = new PropertyPath("ItemsSource"); 

     _tabControl = new TabControl(); 
     _tabControl.Margin = new Thickness(5); 
     _tabControl.DisplayMemberPath = "Header"; 
     _tabControl.SetBinding(ItemsControl.ItemsSourceProperty, itemsSourceBinding); 

     var recRectangle = new Rectangle(); 
     recRectangle.Margin = new Thickness(5); 
     recRectangle.Effect = (Effect)FindResource("MainDropShadowEffect"); 
     recRectangle.Fill = (Brush)FindResource("PanelBackgroundBrush"); 

     var grdGrid = new Grid(); 
     grdGrid.Children.Add(recRectangle); 
     grdGrid.Children.Add(_tabControl); 

     DialogContent = grdGrid; 
    } 
} 
+0

「機能しない」と記述してください。 –

+0

TabItemヘッダーが空です。 – Andre

+0

マージンがないと? –

答えて

5

悪気が、あなたのコードは、あなたの本当の問題からそらす複雑な混乱です。

XAML:

<TabControl ItemsSource="{Binding}" DisplayMemberPath="Header"/> 

コード:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     this.DataContext = new List<TabItemModel> 
     { 
      new TabItemModel 
      { 
       Header = "First" 
      }, 
      new TabItemModel 
      { 
       Header = "Second" 
      }, 
     }; 
    } 
} 

public class TabItemModel 
{ 
    public string Header 
    { 
     get; 
     set; 
    } 
} 

結果:

enter image description here

あなたが DisplayMemberPathを設定すると、あなたがそれを望むとおりに動作することを確認できます簡素化した場合

だから問題はそれではないTabControl.DisplayMemberPathはうまくいかず、複雑すぎるコードのどこかにあります。場所を見つけるまで簡素化してください。

+0

私はいくつかのテストをして、あなたのアイテムが「コントロール」から継承されるならば、それらの問題があることを知りました。 – Andre

関連する問題