gpt4 book ai didi

c# - 如何解决 RibbonApplicationMenu 的绑定(bind)错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:41 26 4
gpt4 key购买 nike

这是 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; }
}

绑定(bind)错误:

System.Windows.Data Error: 40 : BindingExpression path error: 'ImageUri' property not found on 'object' ''EmployeeViewModel' (HashCode=41834681)'. BindingExpression:Path=ImageUri; DataItem='EmployeeViewModel' (HashCode=41834681); target element is 'RibbonApplicationMenu' (Name=''); target property is 'SmallImageSource' (type 'ImageSource')

System.Windows.Data Error: 40 : BindingExpression path error: 'IsDropDownOpen' property not found on 'object' ''RibbonContentPresenter' (Name='PART_ContentPresenter')'. BindingExpression:Path=IsDropDownOpen; DataItem='RibbonContentPresenter' (Name='PART_ContentPresenter'); target element is 'RibbonButton' (Name=''); target property is 'NoTarget' (type 'Object')

我在这里缺少什么?

最佳答案

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

根据错误,“SmallImageSource”属于“ImageSource”类型,ImageSource 不应绑定(bind)到字符串。它应该是一个 Uri。

也为下一个错误

1.target 属性是'NoTarget'(类型'Object')

  1. 目标元素是 'RibbionButton' (Name='');

  2. BindingExpression:Path=IsDropDownOpen;

  3. DataItem='RibbonContentPresenter';

  4. 在“对象”“RibbonContentPresenter”(名称='PART_ContentPresenter')'上找不到“IsDropDownOpen”属性。

  5. BindingExpression路径错误:

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

1 告诉您有一个 NoTarget 属性导致错误

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

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

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

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

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

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

关于c# - 如何解决 RibbonApplicationMenu 的绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32043634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com