【问题标题】:How to solve binding error for RibbonApplicationMenu如何解决 RibbonApplicationMenu 的绑定错误
【发布时间】:2015-08-17 05:58:09
【问题描述】:

这里是 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 路径错误:在“对象”“EmployeeViewModel”(HashCode=41834681)上找不到“ImageUri”属性。绑定表达式:路径=ImageUri; DataItem='EmployeeViewModel' (HashCode=41834681);目标元素是'RibbonApplicationMenu'(名称='');目标属性是“SmallImageSource”(类型“ImageSource”)

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“RibbonContentPresenter”(名称=“PART_ContentPresenter”)上找不到“IsDropDownOpen”属性。绑定表达式:路径=IsDropDownOpen; DataItem='RibbonContentPresenter'(名称='PART_ContentPresenter');目标元素是'RibbonButton'(名称='');目标属性是“NoTarget”(类型“对象”)

我在这里缺少什么?

【问题讨论】:

    标签: c# wpf ribbon ribboncontrolslibrary


    【解决方案1】:

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

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

    同样用于下一个错误

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

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

    2. BindingExpression:Path=IsDropDownOpen;

    3. DataItem='RibbonContentPresenter';

    4. 在 'object' ''RibbonContentPresenter' (Name='PART_ContentPresenter')' 上找不到'IsDropDownOpen' 属性。

    5. BindingExpression 路径错误:

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

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

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

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

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

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

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

    以正确的顺序阅读错误可能会对您有所帮助。由于代码 sn-p 中没有提到有关此 IsDropDownOpen 的任何内容,请编辑您的代码。

    【讨论】:

    • 是的,第一个绑定问题现已解决。但第二个是我无法弄清楚。看起来像是在RibbonControlLibrary
    • IsDropDownOpenRibbon 的财产
    • 该属性的类型是什么?
    • 你能编辑你绑定了 IsDropDownOpen 的代码及其来自代码的属性
    【解决方案2】:

    功能区的DataContextEmployeeViewModelRibbonApplicationMenu 绑定到ImageUri 属性,EmployeeViewModel 中不存在该属性

    <r:RibbonApplicationMenu 
           ItemsSource="{Binding MenuItems, Mode=OneWay}"
           Margin="0, 5, 0, 0"
           SmallImageSource="{Binding ImageUri}">
    </r:RibbonApplicationMenu>
    

    【讨论】:

    • 是的,第一个绑定问题现已解决。但第二个是我无法弄清楚。看起来像是在RibbonControlLibrary
    猜你喜欢
    • 2014-10-02
    • 1970-01-01
    • 2020-08-16
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    相关资源
    最近更新 更多