如何解决RibbonApplicationMenu

时间:2015-08-17 05:58:09

标签: c# wpf ribbon ribboncontrolslibrary

这是xaml:

<r:RibbonWindow x:Class="WpfApplication1.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
        xmlns:local="clr-namespace:WpfApplication1.ViewModel"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>

        <DataTemplate DataType="{x:Type local:RibbonItem}">
            <r:RibbonButton Label="{Binding Label}" SmallImageSource="{Binding ImageUri}" Command="{Binding Command}" />
        </DataTemplate>

    </Window.Resources>

    <StackPanel>

        <StackPanel.Resources>
            <local:EmployeeViewModel x:Key="EmpoyeeViewModel"/>
        </StackPanel.Resources>
        <StackPanel.DataContext>
            <Binding Source="{StaticResource EmpoyeeViewModel}"/>
        </StackPanel.DataContext>

        <r:Ribbon Name="ribbon" >

            <r:Ribbon.ApplicationMenu>
                <r:RibbonApplicationMenu 
                ItemsSource="{Binding MenuItems, Mode=OneWay}"
                Margin="0, 5, 0, 0"
                SmallImageSource="{Binding ImageUri}">
                </r:RibbonApplicationMenu>
            </r:Ribbon.ApplicationMenu>

            <r:RibbonTab Header="Home">
                <r:RibbonGroup x:Name="Clipboard" ItemsSource ="{Binding MenuItems, Mode=OneWay}" >

                    <r:RibbonGroup.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <r:RibbonButton Label="{Binding Label}" 
                                                SmallImageSource="{Binding ImageUri}" Command="{Binding Command}"/>
                            </StackPanel>
                        </DataTemplate>
                    </r:RibbonGroup.ItemTemplate>

                </r:RibbonGroup>
            </r:RibbonTab>

        </r:Ribbon>

    </StackPanel>
    </r:RibbonWindow>

查看型号:

public class EmployeeViewModel : BaseModel
{
    private RelayCommand _SaveCommand;
    private RelayCommand _NewCommand;

    public EmployeeViewModel()
    {
        LoadMenus();
    }

    public ICommand SaveCommand
    {
        get
        {
            return _SaveCommand ?? (_SaveCommand = new RelayCommand(param => Save(), param => CanSave));
        }
    }

    public ICommand NewCommand
    {
        get
        {
            return _NewCommand ?? (_NewCommand = new RelayCommand(param => New())); ;
        }
    }

    public bool CanSave
    {
        get { return true; }
    }

    private void Save() { }

    private void New() { }

    ObservableCollection<RibbonItem> _MenuItems;

    public ObservableCollection<RibbonItem> MenuItems
    {
        get { return _MenuItems; }
    }

    private void LoadMenus()
    {
        _MenuItems = new ObservableCollection<RibbonItem>();

        _MenuItems.Add(new RibbonItem("New", "new-icon.png", NewCommand));
        _MenuItems.Add(new RibbonItem("Save", "save-icon.png", SaveCommand));
    }
}

public class RibbonItem
{
    public RibbonItem(string label, string imageUri, ICommand command)
    {
        Label = label;
        ImageUri = imageUri;
        Command = command;
    }

    public string Label { get; private set; }

    public string ImageUri { get; private set; }

    public ICommand Command { get; private set; }
}

绑定错误:

  
    

System.Windows.Data错误:40:BindingExpression路径错误:&#39; ImageUri&#39;在&#39; object&#39;上找不到的属性&#39;&#39; EmployeeViewModel&#39; (的HashCode = 41834681)&#39 ;. BindingExpression:路径= ImageUri;的DataItem =&#39; EmployeeViewModel&#39; (的HashCode = 41834681);目标元素是&#39; RibbonApplicationMenu&#39; (名称=&#39;&#39);目标属性是&#39; SmallImageSource&#39; (键入&#39; ImageSource&#39;)

         

System.Windows.Data错误:40:BindingExpression路径错误:&#39; IsDropDownOpen&#39;在&#39; object&#39;上找不到的属性&#39;&#39; RibbonContentPresenter&#39; (名称=&#39; PART_ContentPresenter&#39;)&#39 ;. BindingExpression:路径= IsDropDownOpen;的DataItem =&#39; RibbonContentPresenter&#39; (名称=&#39; PART_ContentPresenter&#39);目标元素是&#39; RibbonButton&#39; (名称=&#39;&#39);目标财产是“NoTarget&#39; (键入&#39;对象&#39;)

  

我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

当DataContext错误或根本未设置时,会发生此问题。

根据错误&#34; SmallImageSource&#34;属于&#34; ImageSource&#34;并且不应将ImageSource绑定到字符串。它应该是一个Uri。

还有下一个错误

1.target property is&#39; NoTarget&#39; (键入&#39;对象&#39;)

  1. 目标元素是&#39; RibbionButton&#39; (名称=&#39;&#39);

  2. BindingExpression:路径= IsDropDownOpen;

  3. 的DataItem =&#39; RibbonContentPresenter&#39 ;;

  4. &#39; IsDropDownOpen&#39;在&#39; object&#39;上找不到的属性&#39;&#39; RibbonContentPresenter&#39; (名称=&#39; PART_ContentPresenter&#39;)&#39;

  5. BindingExpression路径错误:

  6. System.Windows.Data错误:40:

  7. 1告诉您存在导致错误的NoTarget属性

    2告诉您NoTarget属性位于RibbionButton元素

    3告诉你导致问题的绑定表达是{Binding Path = IsDropDownOpen}

    4告诉你RibbonContentPresenter元素后面的DataItem / DataContext是一个数据类型为Char的项目

    5告诉你实际的问题:在RibbonContentPresenter类型的对象上没有名为IsDropDownOpen的属性

    6只是告诉你它是一个绑定错误

    以正确的顺序读取错误可能对您有所帮助。由于此IsDropDownOpen的代码段中没有提及,请编辑您的代码。

答案 1 :(得分:1)

功能区的DataContextEmployeeViewModelRibbonApplicationMenu绑定到ImageUri属性,EmployeeViewModel

中不存在
<r:RibbonApplicationMenu 
       ItemsSource="{Binding MenuItems, Mode=OneWay}"
       Margin="0, 5, 0, 0"
       SmallImageSource="{Binding ImageUri}">
</r:RibbonApplicationMenu>