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

标签 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.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”)

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

  • BindingExpression:Path=IsDropDownOpen;

  • DataItem='RibbonContentPresenter';

  • 在“对象”“RibbonContentPresenter”(Name='PART_ContentPresenter')'上找不到“IsDropDownOpen”属性。

  • BindingExpression 路径错误:

  • System.Windows.Data 错误:40:

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

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

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

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

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

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

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

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

    相关文章:

    c# - 动态范围日期数据注释验证属性 c#

    c# - WinForms 中缺少 VM_KEYPRESS?

    c# - 如何设置字符串属性 C# EF 的分隔词的最大长度

    .net - ListBox和ListView有什么区别

    matlab - 使用丝带的瀑布图

    c# - HtmlTable、HtmlTableRow、HtmlTableCell - 创建 thead、tbody 和 tfoot

    c# - WCF 服务和防火墙...有任何问题吗?

    c# - 如何使笔刷平滑而中间没有线条

    wpf - 如何以编程方式切换功能区选项卡?

    .net - 如何隐藏功能区控件的上下文菜单