Pregunta

I'm up against a very simple problem I guess, however I find no way to solve it.

I basically want to create a ribbon tab with all it´s content from a class:

class RibbonTabHome
{
    public RibbonTab RibbonTab_Container;

    public RibbonTabHome()
    {
        RibbonTab_Container = new RibbonTab() { Header = "Wont appear!" };
    }
}

I then call the class and add the tab to the ribbon:

public MainWindow()
{
    InitializeComponent();

    RibbonTabHome asd = new RibbonTabHome();

    RibbonMain.Items.Add(asd.RibbonTab_Container);
}

For some reason only the tab container itself is added, the header text is missing!

However this statement does its work perfectly!

RibbonMain.Items.Add(new RibbonTab() { Header = "Appearing"});

What am I missing?

Best regards!

¿Fue útil?

Solución

What am I missing?

It seems that you are missing a great deal. WPF is simply not written like that. In WPF, we manipulate data elements, not UI elements. We declare DataTemplates that define how our data should be rendered in the UI, so to add a new TabItem in a TabControl, you add a new data item into a data collection that is data bound to the ItemsSource property of the TabControl.

So you can create a RibbonTab from classes, but those classes do not manually build the UI elements, instead they provide the data for the controls. For example, to add a Button, you'd need a class that had a couple of string properties to data bind to the Button.Content and ToolTip properties. It would also need an ICommand property to data bind to the actual Command property. To add a new Button, you just add another of these classes into the relevant collection.

As to what class goes in what collection, well that is far to long a story to cover here. Luckily, the topic of data binding RibbonTab controls has been widely discussed online, so you'll have plenty of good sources for that. Don't forget MSDN is a great resource... there are plenty of relevant links from the Ribbon Overview page.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top