在C#中设置TabControl的DisplayMemberPath

And*_*dre 0 c# wpf binding tabcontrol

我用C#代码创建了一个TabControl.我将其ItemsSource绑定到一个集合并设置边距.出于某种原因,设置其DisplayMemberPath不会工作.

_tabControl = new TabControl();
_tabControl.Margin = new Thickness(5);
_tabControl.DisplayMemberPath = "Header";
_tabControl.SetBinding(ItemsControl.ItemsSourceProperty, itemsSourceBinding);
Run Code Online (Sandbox Code Playgroud)

集合中的每个项目都有一个名为"Header"的属性.

为什么这不起作用?

安德烈

编辑:这是所有相关的代码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ken*_*art 5

没有冒犯,但你的代码是一个令人费解的混乱,分散了你的真实问题.如果您简化,您将看到该设置DisplayMemberPath完全符合您的要求:

XAML:

<TabControl ItemsSource="{Binding}" DisplayMemberPath="Header"/>
Run Code Online (Sandbox Code Playgroud)

码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

所以,问题TabControl.DisplayMemberPath不是不起作用 - 它在你过于复杂的代码中的其他地方.简化,直到找到位置.