C# 如何解决RibbonaApplication菜单的绑定错误
这里是xaml: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
<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”)
按正确的顺序读取错误可能会对您有所帮助。由于代码段中没有提到有关此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的代码吗?它是默认值