2

I am learning WPF and I find out that Modern-UI is really awesome to build WPF applications. However, I could not find a way to do binding to the content of menu links (the DisplayName attribute) with Modern-UI. Currently, I can change the DisplayName attribute in the code behind only. Any help is appreciated! Thank you!

<Grid Style="{StaticResource ContentRoot}">
    <mui:ModernTab x:Name="ModernTab" Layout="List" SelectedSource="/Pages/Messages/Inbox.xaml">
        <mui:ModernTab.Links>
            <mui:Link x:Name="NewMessage"  DisplayName="New Message" Source="/Pages/Messages/NewMessage.xaml" />
            <mui:Link x:Name="InboxLink" DisplayName="Inbox"  Source="/Pages/Messages/Inbox.xaml" />
            <mui:Link x:Name="SentLink" DisplayName="Sent" Source="/Pages/Messages/Sent.xaml" />
            <mui:Link x:Name="SystemMessagesLink" DisplayName="System Messages" Source="/Pages/Messages/SystemMessages.xaml" />
            <mui:Link x:Name="AccountSettingsLink" DisplayName="Account Settings" Source="/Pages/Admin/UserAccount.xaml" />
        </mui:ModernTab.Links>
    </mui:ModernTab>

h2nghia
  • 432
  • 5
  • 15
  • I believe your problem is the DisplayName is not a dependency property. Prehaps this link can help you. http://www.codeproject.com/Articles/71348/Binding-on-a-Property-which-is-not-a-DependencyPro – mrsargent Nov 10 '15 at 02:11
  • Thanks for the link @mrsargent. I am looking into that to find an idea. – h2nghia Nov 16 '15 at 23:02

2 Answers2

0

You need to bind ModernTab.Links into a link collection in your viewmodel.

If you are not using MVVM, you need to create a link collection with links and your changed display names in it and assign this link collection to your ModernTab.Links.

Huseyin Yagli
  • 9,896
  • 6
  • 41
  • 50
0

Rise from your grave.....

I've just had this problem and it's actually not too bad. Both the Title Links - the little ones like Settings in the top of the frame - and the actual Menu are dependency properties and can be bound in your Viewmodel.

Here's how I declare mine -

private LinkGroupCollection _menuLinkGroups = new LinkGroupCollection();

public LinkGroupCollection MenuLinkGroups
{
    get => _menuLinkGroups;
    set => SetProperty(ref _menuLinkGroups, value);
}

private LinkCollection _titleLinks = new LinkCollection();

public LinkCollection TitleLinks
{
    get => _titleLinks;
    set => SetProperty(ref _titleLinks, value);
}

Then I can populate them like so -

private void ConstructLinks()
{
    if (MenuLinkGroups.Any())
    {
        MenuLinkGroups.Clear();
        TitleLinks.Clear();
    }

    var group = new LinkGroup {DisplayName = "Home"};
    var link = new Link { DisplayName = "Home", Source = MenuLinks.Home };

    group.Links.Add(link);
    MenuLinkGroups.Add(group);
    group = new LinkGroup{DisplayName = "Menu 1"};
    link = new Link {DisplayName = "Menu Item 1", Source = MenuLinks.Item1};
    group.Links.Add(link);
    link = new Link {DisplayName = "Menu Item 2", Source = MenuLinks.Item2};
    group.Links.Add(link);
    MenuLinkGroups.Add(group);


    group = new LinkGroup { DisplayName = "Menu 2" };

    link = new Link { DisplayName = "Menu Item3", Source = MenuLinks.Item3 };
    group.Links.Add(link);
    link = new Link {DisplayName = "Menu Item 4", Source = MenuLinks.Item4};
    group.Links.Add(link);
    MenuLinkGroups.Add(group);

    // Settings
    link = new Link { DisplayName = "Settings", Source = MenuLinks.Settings };
    TitleLinks.Add(link);
}

The MenuLinks class is just a static class containing the various URIs to my Views.

And finally bind them in the XAML.

<mui:ModernWindow x:Class="MyApp.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:mui="http://firstfloorsoftware.com/ModernUI"
                  xmlns:prism="http://prismlibrary.com/"
                  prism:ViewModelLocator.AutoWireViewModel="True"
                  Title="{Binding Title}" IsTitleVisible="True"
                  ContentSource="/Views/Introduction.xaml"
                  Width="800" Height="700" WindowState="Maximized" SizeToContent="Manual"
                  TitleLinks ="{Binding TitleLinks}"
                  MenuLinkGroups ="{Binding MenuLinkGroups}">
</mui:ModernWindow>

It's worth pointing out that ContentSource is also a Dependency Property so you can bind that, too. If you fancy a little ghetto navigation, that is.

Rich Bryant
  • 865
  • 10
  • 27