以下是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路径错误:在'object‘'EmployeeViewModel’(HashCode=41834681)‘上找不到'ImageUri’属性。BindingExpression:Path=ImageUri;DataItem='EmployeeViewModel‘(HashCode=41834681);目标元素为'RibbonApplicationMenu’(名称=‘’);目标属性为'SmallImageSource‘(键入'ImageSource') System.Windows.Data错误: 40 : BindingExpression路径错误:在'object‘'RibbonContentPresenter’(Name='PART_ContentPresenter')‘上找不到'IsDropDownOpen’属性。BindingExpression:Path=IsDropDownOpen;DataItem='RibbonContentPresenter‘(Name='PART_ContentPresenter');目标元素是'RibbonButton’(名称=‘’);目标属性是'NoTarget‘(类型'Object')
我在这里错过了什么?
发布于 2015-08-17 08:23:57
当DataContext错误或根本没有设置它时,就会出现这个问题。
根据错误,"SmallImageSource“类型为" ImageSource”,ImageSource不应绑定到字符串。它应该是一个Uri。
也适用于下一个错误
1.目标属性为“NoTarget”(键入“Object”)
1告诉您,有一个NoTarget属性导致错误。
2告诉您,NoTarget属性位于RibbionButton元素上。
3告诉您导致问题的绑定表达式是{bindingPath=IsDropDownOpen}
4告诉您,RibbonContentPresenter元素后面的DataItem/DataContext是数据类型Char的一项
5告诉您实际问题:在类型为IsDropDownOpen的对象上没有名为RibbonContentPresenter的属性。
6只告诉你这是一个绑定错误
按正确的顺序阅读错误可能会对您有所帮助。由于这个IsDropDownOpen的代码片段中没有提到任何内容,请编辑您的代码。
https://stackoverflow.com/questions/32043634
复制相似问题