如何解决绑定错误的RibbonApplicationMenu
本文关键字:绑定 错误 RibbonApplicationMenu 解决 何解决 | 更新日期: 2023-09-27 18:08:27
这里是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。数据错误:40:BindingExpression路径错误:'ImageUri'属性未在'对象''EmployeeViewModel' (HashCode=41834681)'上找到。BindingExpression:路径= ImageUri;DataItem = ' EmployeeViewModel ' (HashCode = 41834681);目标元素是'RibbonApplicationMenu' (Name= ");目标属性是'SmallImageSource'(类型为'ImageSource')
System.Windows。数据错误:40:BindingExpression路径错误:'IsDropDownOpen'属性未在'对象' " RibbonContentPresenter' (Name='PART_ContentPresenter')'上找到。BindingExpression:路径= IsDropDownOpen;DataItem = ' RibbonContentPresenter ' (Name = ' PART_ContentPresenter ');目标元素是'RibbonButton' (Name= ");目标属性为'NoTarget'(类型为'Object')
我在这里错过了什么?
此问题发生在DataContext错误或根本没有设置时。
根据错误" smalllimagesource "的类型是"ImageSource"和ImageSource不应该绑定到一个字符串。它应该是Uri。
同样适用于下一个错误
1。目标属性为'NoTarget'(类型为'Object')
-
目标元素为'RibbionButton' (Name= " ");
-
BindingExpression:路径= IsDropDownOpen;
-
DataItem ="RibbonContentPresenter";
-
在'RibbonContentPresenter' (Name='PART_ContentPresenter')'上找不到'IsDropDownOpen'属性
-
BindingExpression路径错误:
-
System.Windows。数据错误:40:
1告诉您有一个NoTarget属性导致错误
2告诉您NoTarget属性位于RibbionButton元素
3告诉你导致问题的绑定表达式是{binding Path= isdropdownnopen}
4告诉你RibbonContentPresenter元素背后的DataItem/DataContext是一个数据类型为Char的项
5告诉你实际的问题:在类型为RibbonContentPresenter的对象上没有名为isdropdownnopen的属性
6只是告诉你这是一个绑定错误
按正确顺序阅读错误可能会对您有所帮助。由于代码片段中没有提到此IsDropDownOpen,请编辑您的代码
Ribbon的DataContext
是EmployeeViewModel
, RibbonApplicationMenu
与ImageUri
结合,这在EmployeeViewModel
中不存在
<r:RibbonApplicationMenu
ItemsSource="{Binding MenuItems, Mode=OneWay}"
Margin="0, 5, 0, 0"
SmallImageSource="{Binding ImageUri}">
</r:RibbonApplicationMenu>