2
votes

Here is 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>

View Model:

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

Binding Errors:

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')

What is I am missing here?

2

2 Answers

1
votes

This problem occurs when either DataContext is wrong or if it is not set at all.

According to the error "SmallImageSource" is of type "ImageSource" and ImageSource should not be binded to a string. It should be a Uri.

Also for the next error

1.target property is 'NoTarget' (type 'Object')

  1. target element is 'RibbionButton' (Name='');

  2. BindingExpression:Path=IsDropDownOpen;

  3. DataItem='RibbonContentPresenter';

  4. 'IsDropDownOpen' property not found on 'object' ''RibbonContentPresenter' (Name='PART_ContentPresenter')'.

  5. BindingExpression path error:

  6. System.Windows.Data Error: 40 :

1 tells you that there is a NoTarget property causing the error

2 tells you that the NoTarget property is on a RibbionButton element

3 tells you the binding express causing the problem is {Binding Path=IsDropDownOpen}

4 tells you the DataItem/DataContext behind the RibbonContentPresenter element is an item of data type Char

5 tells you the actual problem with this: there is no property named IsDropDownOpen on the object of type RibbonContentPresenter

6 just tells you it's a binding error

Reading the error in correct sequence may help you. As there is nothing mentioned in the code snippet about this IsDropDownOpen , please edit your code.

1
votes

The Ribbon's DataContext is the EmployeeViewModel and the RibbonApplicationMenu binds to ImageUri property, which does not exist in EmployeeViewModel

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