简体   繁体   中英

Wpf binding instance of a class to tabitem

I want to bind the Property Title to my tabitem header . My architecture looks like this:

Image

In my mainwindow.xaml file I have this code:

<Controls:MetroWindow x:Class="ConfigManager.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" 
    Title="ConfigManager">

<DockPanel>
    <TabControl Controls:TabControlHelper.IsUnderlined="True">
        <TabItem DataContext="{Binding UserManager}" Header="{Binding Title}"">

        </TabItem>
        <TabItem DataContext="{Binding PrinterManager}" Header="{Binding Title}">

        </TabItem>
        <TabItem Header="TabItem">

        </TabItem>
    </TabControl>
</DockPanel>
</Controls:MetroWindow>

My MainWindow.xaml.cs :

namespace ConfigManager
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{

    public ManagerRoot ManagerRoot { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        ManagerRoot = new ManagerRoot();
        DataContext = ManagerRoot;
    }
}
}

My ManagerRoot:

namespace ConfigManager
{
[Serializable]
public class ManagerRoot
{
    [XmlElement("UserManager")]
    public UserManager UserManager = new UserManager();

    [XmlElement("PrinterManager")]
    public PrinterManager PrinterManager = new PrinterManager();
}
}

My UserManager (PrinterManager is the same):

[XmlIgnore]
    private string _title = "User";
    [XmlAttribute("Title")]
    public string Title
    {
        get { return this._title; }
        set { this._title = value; }
    }

And thats the error I get:

System.Windows.Data Error: 40 : BindingExpression path error: 'Title' property not found on 'object' ''ManagerRoot' (HashCode=20501377)'. BindingExpression:Path=Title; DataItem='ManagerRoot' (HashCode=20501377); target element is 'TabItem' (Name=''); target property is 'Header' (type 'Object') System.Windows.Data Error: 40 : BindingExpression path error: 'UserManager' property not found on 'object' ''ManagerRoot' (HashCode=20501377)'. BindingExpression:Path=UserManager; DataItem='ManagerRoot' (HashCode=20501377); target element is 'TabItem' (Name=''); target property is 'DataContext' (type 'Object')

Maybe someone of you can help me... Im trying it since 3 hours.

You've got several problems:

  1. ManagerRoot needs to use properties

In your code, the UserManager and PrinterManager members of ManagerRoot are actually fields here, not properties. You can't bind to fields. Change them to properties instead. This is your main problem.

  1. Raise property change notifications

Maybe your real code does this in some way and you haven't shown it, but in order for binding to work well, you should be triggering property change notifications for all properties you bind to. That means ManagerRoot, UserManager, and PrinterManager should all generate property change notifications for their properties. You can probably get away without it in your example, but if you ever tried to change the title property, or replace ManagerRoot, PrinterManager, or UserManager with other objects at runtime while your panel is open, the change won't take effect on your panel without property change notifications being generated. There are lots of good articles on understanding property change notifications out there, and I don't have a link to one on hand, but if you do a search for information on the topic, you should find a lot of good references and examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM