如何使用的ItemsSource和的DataTemplates呈现从数据对象功能区 [英] How to render a Ribbon from data objects using ItemsSource and DataTemplates

查看:192
本文介绍了如何使用的ItemsSource和的DataTemplates呈现从数据对象功能区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着提供一个数据模板到我的色带。

Im trying to supply a data template to my ribbon.

色带声明如下,并连有一个ItemTemplate。

The ribbon is declared as following, and has an ItemTemplate attached to it.

<r:Ribbon Name="RibbonMain"
          ItemTemplate="{StaticResource HomeRibbonTabTemplate}">
</r:Ribbon>



的DataTemplate如下:

The Datatemplate is the following:

<Window.Resources>
    <DataTemplate DataType="{x:Type local:RibbonContainer}" 
                  x:Key="HomeRibbonTabTemplate">
        <r:RibbonTab Header="{Binding Path=HeaderName}">
            <r:RibbonGroup Header="{Binding Path=GroupName}">
            </r:RibbonGroup>
        </r:RibbonTab>
    </DataTemplate>
</Window.Resources>



我再附上的ItemsSource:

I do then attach the ItemsSource:

    public MainWindow()
    {
        InitializeComponent();

        var RibbonTabData = new ObservableCollection<RibbonContainer>();
        RibbonTabData.Add(new RibbonContainer("HeaderName", "GroupName"));
        RibbonMain.ItemsSource = RibbonTabData;
    }



最后类:(这只是包含两个字符串字段)

Lastly the class: (Which just contains two string fields)

class RibbonContainer
{
    public string HeaderName
    {
        get;
        set;
    }

    public string GroupName
    {
        get;
        set;
    }

    public RibbonContainer(string _headername, string _groupname)
    {
        HeaderName = _headername;
        GroupName = _groupname;
    }
}



我得到展示的完全限定类的不起眼的结果命名标签头也不是ribbongroup表现。 (这是DataTemplate中应该解决什么?)
怎么办?

I get the unimpressive result of showing the fully qualified class name in the tab header and neither is the ribbongroup showing. (This is what the datatemplate should solve?) What to do?

最好的问候

推荐答案

我不完全知道从哪里开始,但也许有一个简短的警告,在试图创建一个 RibbonControl 绑定和数据项,您的真正的是在自己开辟的呐喊驴的巨大即可。这是因为设计的代码,它的开发者使用非常规的模式对于一些并没有充分记录如何做的事情吧。一些最好的来源将通过这个网站上搜索找到。

I'm not exactly sure where to start, but perhaps with a short warning that, in trying to create a RibbonControl totally from data Binding and data items, you really are opening up a huge can of whoop ass on yourself. This is because the developers that designed the code for it used unconventional patterns for some of it and failed to adequately document how to do things with it. Some of the best sources will be found by searching on this website.

所以无论如何,如果你为一个痛苦的,艰苦的斗争,继续读下去。你的第一个错误是试图用一个的DataTemplate RibbonTab ,因为它是延伸的 System.Windows.Controls.ItemsControl 并因此需要的 HierarchicalDataTemplate 。你的第二个错误是在声明 RibbonTab 在模板中,如在评论中提及@devhedgehog。

So anyway, if you're up for a painful, uphill struggle, read on. Your first mistake was trying to use a DataTemplate for the RibbonTab because it is extends System.Windows.Controls.ItemsControl and therefore requires a HierarchicalDataTemplate. Your second mistake was declaring the RibbonTab inside the template, as @devhedgehog mentioned in a comment.

您第三个错误是设置 X:键为您的价值的DataTemplate ,并把它应用到 Ribbon.ItemsTemplate 属性......我知道,我知道......一个明智的足够的事情的如果这不是一个 RibbonControl 。你得问那些开发商为的为什么的不工作,但你最好还是先接受它不会和适应你的代码。你只需要删除 X:键值和 Ribbon.ItemsTemplate 属性,让框架应用模板

You third mistake was setting the x:Key value for your DataTemplate and applying it to the Ribbon.ItemsTemplate property... I know, I know... a sensible enough thing to do if this wasn't a RibbonControl. You'll have to ask those developers as to why that doesn't work, but you're better off just accepting that it doesn't and adapting your code. You just need to remove the x:Key value and the Ribbon.ItemsTemplate property and let the Framework apply the template implicitly.

现在,如果你想多个 RibbonGroup ,那么你的第四个错误被定义,在模板的 RibbonTab 。如果你要正确地做到这一点,那么你的数据类将需要匹配丝带 UI元素的各个层面。这样,我的意思是,你需要创建一个 RibbonGroupData 类了。那类需要在UI中的数据提供给每个 RibbonButton RibbonButtonData 对象的集合。所以,你应该结束了,像这样:

Now if you ever want more than one RibbonGroup, then your fourth mistake was defining that in the template for the RibbonTab. If you're going to do this properly, then your data classes will need to match the various levels of UI elements in the Ribbon. By this, I mean that you need to create a RibbonGroupData class too. That class needs a collection of RibbonButtonData objects that supply the data to each RibbonButton in the UI. So you should end up with something like this:

public class RibbonTabData : BaseDataType
{
    private string name = string.Empty;
    private ObservableCollection<RibbonGroupData> ribbonGroupData = new ObservableCollection<RibbonGroupData>();

    public string Name
    {
        get { return name; }
        set { name = value; NotifyPropertyChanged("Name"); }
    }

    public ObservableCollection<RibbonGroupData> RibbonGroupData
    {
        get { return ribbonGroupData; }
        set { ribbonGroupData = value; NotifyPropertyChanged("RibbonGroupData"); }
    }
}

public class RibbonGroupData : BaseDataType
{
    private string name = string.Empty;
    private ObservableCollection<RibbonButtonData> ribbonButtonData = new ObservableCollection<RibbonButtonData>();

    public string Name
    {
        get { return name; }
        set { name = value; NotifyPropertyChanged("Name"); }
    }

    public ObservableCollection<RibbonButtonData> RibbonButtonData
    {
        get { return ribbonButtonData; }
        set { ribbonButtonData = value; NotifyPropertyChanged("RibbonButtonData"); }
    }
}

public class RibbonButtonData : BaseDataType
{
    private string name = string.Empty;

    public string Name
    {
        get { return name; }
        set { name = value; NotifyPropertyChanged("Name"); }
    }
}



BaseDataType 类只是实现了 INotifyPropertyChanged的接口。当然,你需要为 ICommand的添加额外的属性和图像源,等你甚至可能需要不同的 RibbonButtonData 针对不同类型的 RibbonButton 和那么你需要一个共同的 RibbonButtonBaseData 类不同属性的类,他们所有扩展,让您的集合可以包含各种不同类型的在一起。因此,有很多更适合你做,但鉴于该示例代码,你可以在丝带是这样显示的:

The BaseDataType class just implements the INotifyPropertyChanged interface. Of course, you'd need to add extra properties for ICommands and image sources, etc. You might even need different RibbonButtonData classes with different properties for different types of RibbonButtons and then you'd need a common RibbonButtonBaseData class that they all extended, so your collection could contain all the different types together. So there's lots more for you to do, but given this example code, you could display it in the Ribbon like this:

<Ribbon:RibbonWindow.Resources>
    <HierarchicalDataTemplate DataType="{x:Type DataTypes:RibbonTabData}" 
        ItemsSource="{Binding RibbonGroupData}">
        <TextBlock Text="{Binding Name}" />
    </HierarchicalDataTemplate>
    <DataTemplate DataType="{x:Type DataTypes:RibbonButtonData}">
        <Ribbon:RibbonButton Label="{Binding Name}" 
            LargeImageSource="/WpfRibbonApplication1;component/Images/LargeIcon.png" />
    </DataTemplate>
    <HierarchicalDataTemplate DataType="{x:Type DataTypes:RibbonGroupData}" 
        ItemsSource="{Binding RibbonButtonData}">
        <Ribbon:RibbonGroup Header="{Binding Name}" />
    </HierarchicalDataTemplate>
</Ribbon:RibbonWindow.Resources>
<Ribbon:Ribbon x:Name="Ribbon" ItemsSource="{Binding RibbonTabData}" />



在设置为的DataContext 窗口,我可以添加一些假的数据来测试它的所有作品:

Now in the view model that is set as the DataContext for the Window, I can add some dummy data to test that it all works:

RibbonTabData.Add(new RibbonTabData() { Name = "Tab 1", RibbonGroupData = new ObservableCollection<RibbonGroupData>() { new RibbonGroupData() { Name = "Group 1", RibbonButtonData = new ObservableCollection<RibbonButtonData>() { new RibbonButtonData() { Name = "Button 1" }, new RibbonButtonData() { Name = "Button 2" }, new RibbonButtonData() { Name = "Button 3" } } }, new RibbonGroupData() { Name = "Group 2", RibbonButtonData = new ObservableCollection<RibbonButtonData>() { new RibbonButtonData() { Name = "Button 1" }, new RibbonButtonData() { Name = "Button 2" } } } } });
RibbonTabData.Add(new RibbonTabData() { Name = "Tab 2" });
RibbonTabData.Add(new RibbonTabData() { Name = "Tab 3" });

和我们得到这样的:

< IMG SRC =http://i.stack.imgur.com/0rGdU.pngALT =在这里输入的形象描述>

然而,即使这是很有帮助的开始,你还有一个的很多的更多的工作要做。

However, even with this helpful start, you've still got a lot more work to do.

这篇关于如何使用的ItemsSource和的DataTemplates呈现从数据对象功能区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆