I create a TabControl in C# code. I bind its ItemsSource to a collection and set margin.
For some reason, to set its DisplayMemberPath wont work.
_tabControl = new TabControl();
_tabControl.Margin = new Thickness(5);
_tabControl.DisplayMemberPath = "Header";
_tabControl.SetBinding(ItemsControl.ItemsSourceProperty, itemsSourceBinding);
Each item in the collection have a property called “Header”.
Why does that not work?
André
EDIT:
Here is all relevant code:
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;
}
}
If you clean up and simplify your code, you’ll see that setting
DisplayMemberPathworks exactly as you want it to:XAML:
Code:
Result:
So, the problem is not that
TabControl.DisplayMemberPathdoesn’t work – it’s somewhere else in your code. Simplify until you find where.