0

I'm new to WPF and am trying to build an MRU (most recently used items) menu with the Fluent.Ribbon library.

Some digging in example code and the internet lead me to this point where I can generate the menuitems in splitbutton based on my list or recently used items. But I can't figure out how to wire these items up to by OpenCommand

class MainWindowVM : NotifyingObject
{
    private ICommand openCommand;
    private ObservableCollection<String> recentFileNames = new ObservableCollection<string>() { "Bellerophon", "Athena" };
    public ICommand OpenCommand
    {
        get => openCommand;
    }
    public ObservableCollection<String> RecentFileNames
    {
        get => recentFileNames;
    }
}

The Viewmodel is applied to the RibbonWindow

<Window.DataContext>
    <vm:MainWindowVM />
</Window.DataContext>

And the current state of my Button is this:

<Fluent:SplitButton Header="Open..." Size="Middle"
        Command="{Binding OpenCommand}"
        ItemsSource="{Binding Path=RecentFileNames}">
    <Fluent:SplitButton.Icon>
        <Image Source="Icons/folder-open-document.png" />
    </Fluent:SplitButton.Icon>
    <Fluent:SplitButton.LargeIcon>
        <Image Source="Icons/folder-open-document.png" />
    </Fluent:SplitButton.LargeIcon>
    <Fluent:SplitButton.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"/>
        </DataTemplate>
    </Fluent:SplitButton.ItemTemplate>
    <Fluent:SplitButton.ItemContainerStyle>
        <Style TargetType="{x:Type Fluent:MenuItem}">
            <Setter Property="Command"
                Value="{Binding DataContext.OpenCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Fluent:MenuItem}, AncestorLevel=1}}" />
            <!--Setter Property="CommandParameter" Value="{}" /-->
        </Style>
    </Fluent:SplitButton.ItemContainerStyle>
</Fluent:SplitButton>

I've adapted this code from another SO question running on normal menuitems. But my command isn't getting called. I know that I'll eventually have to pass a parameter to distinguish between the different items but I can't even get it to run the plain command.

dymanoid
  • 14,771
  • 4
  • 36
  • 64
Kempeth
  • 1,856
  • 2
  • 22
  • 37
  • Do you see any data binding errors in the Visual Studio's Output window? – dymanoid Jul 11 '18 at 08:50
  • @dymanoid `System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Fluent.MenuItem', AncestorLevel='1''. BindingExpression:Path=DataContext.OpenCommand; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')` – Kempeth Jul 11 '18 at 08:58
  • You should add this information into your question. It's important enough to not be "just a comment". And it's of course a very nice hint to a solution. – dymanoid Jul 11 '18 at 09:00
  • 1
    And the solution is [already known](https://stackoverflow.com/a/25647539/2846483). It has nothing to do with the Fluent.Ribbon, it's a general problem with popups/context menus. – dymanoid Jul 11 '18 at 09:02

0 Answers0