Search code examples
c#wpfmodern-ui

How to get the DisplayName of the selected ModernTab in ModernUI?


I create dynamically ModernTab in the code behind with their informations (DisplayName and URI source).

Initialization of the ModernTab:

<Grid Style="{StaticResource ContentRoot}">
    <mui:ModernTab Layout="List" Name="listEcole"
                   PreviewMouseLeftButtonUp="ModTab_PreviewMouseLeftButtonUp"/>
</Grid> 

Code behind :

List<string> listEcoles = MainWindow._RE.ListEcoles();
foreach(string nomEcole in listEcoles)
{
    listEcole.Links.Add(new Link() 
    {
        DisplayName = nomEcole, 
        Source = new Uri("/Controles/EcoleControl.xaml", UriKind.Relative) 
    }); 
}

My problem is that i want to know the DisplayName of the selected link to put it in a property and use it in another UserControl.


Solution

  • Try this:

    var selectedLink = listEcole.Links.FirstOrDefault(x => x.Source == listEcole.SelectedSource);
    if (selectedLink != null)
    {
        string selectedDisplayName = selectedLink.DisplayName;
    }
    

    It should give you a reference to the currently selected Link in the ModernTab.