C# 如何解决RibbonaApplication菜单的绑定错误

C# 如何解决RibbonaApplication菜单的绑定错误,c#,wpf,ribbon,ribboncontrolslibrary,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.C

这里是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; }
}
公共类EmployeeViewModel:BaseModel
{
专用中继命令_SaveCommand;
私人关系司令部;
公共雇员视图模型()
{
加载菜单();
}
公共ICommand SaveCommand
{
得到
{
返回_SaveCommand??(_SaveCommand=newrelaycommand(param=>Save(),param=>CanSave));
}
}
公共ICommand NewCommand
{
得到
{
返回_NewCommand???(_NewCommand=newrelaycommand(param=>new());
}
}
公共图书馆可以保存
{
获取{return true;}
}
私有void Save(){}
私有void New(){}
可观察采集菜单;
公共可观测集合菜单项
{
获取{return\u MenuItems;}
}
私有void加载菜单()
{
_MenuItems=新的ObservableCollection();
_添加(新的RibbonItem(“new”,“new icon.png”,NewCommand));
_添加(新的RibbonItem(“Save”,“Save icon.png”,SaveCommand));
}
}
公共阶级主义
{
公共RibbonItem(字符串标签、字符串imageUri、ICommand命令)
{
标签=标签;
ImageUri=ImageUri;
命令=命令;
}
公共字符串标签{get;private set;}
公共字符串ImageUri{get;private set;}
公共ICommand命令{get;private set;}
}
绑定错误:

System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“EmployeeViewModel”(HashCode=41834681)“上找不到“ImageUri”属性。BindingExpression:Path=ImageUri;DataItem='EmployeeViewModel'(HashCode=41834681);目标元素是“RibbonApplicationMenu”(名称=“”);目标属性为“SmallImageSource”(类型为“ImageSource”)

System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“RibbonContentPresenter”(Name='PART\U ContentPresenter')上找不到“IsDropDownOpen”属性。BindingExpression:Path=IsDropDownOpen;DataItem='RibbonContentPresenter'(Name='PART\u ContentPresenter');目标元素是“RibbonButton”(名称=“”);目标属性为“NoTarget”(类型为“Object”)


这里缺少什么?

当DataContext错误或根本没有设置时,就会出现此问题

根据错误,“SmallImageSource”的类型为“ImageSource”,不应将ImageSource绑定到字符串。它应该是一个Uri

还有下一个错误

1.目标属性为“NoTarget”(类型为“Object”)

  • 目标元素是“RibbionButton”(名称=“”)

  • BindingExpression:Path=IsDropDownOpen

  • DataItem='RibbonContentPresenter'

  • 在“对象”“RibbonContentPresenter”(Name='PART\U ContentPresenter')上找不到“IsDropDownOpen”属性

  • BindingExpression路径错误:

  • System.Windows.Data错误:40:

  • 1告诉您存在导致错误的NotTarget属性

    2告诉您NotTarget属性位于RibbionButton元素上

    3告诉您导致问题的绑定表达式是{binding Path=IsDropDownOpen}

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

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

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


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

    功能区的
    DataContext
    EmployeeViewModel
    ,而
    RibbonApplicationMenu
    绑定到
    ImageUri
    属性,该属性在
    EmployeeViewModel

    <r:RibbonApplicationMenu 
           ItemsSource="{Binding MenuItems, Mode=OneWay}"
           Margin="0, 5, 0, 0"
           SmallImageSource="{Binding ImageUri}">
    </r:RibbonApplicationMenu>
    
    
    
    是,第一个绑定问题现已解决。但第二个问题是我无法理解。看起来它在
    RibbonControl库中
    Yes,现在解决了第一个绑定问题。但第二个问题是我无法理解。看起来像是在
    RibbonControl Library
    IsDropDownOpen
    Ribbon
    的属性,该属性的类型是什么?您可以编辑绑定了IsDropDownOpen且其属性来自code的代码吗?它是默认值