1

I encounter some issues with my project. I have a moderntab that I fill of link by a request into a database. The result of my request return a list of string that will be the displayname of the link of my moderntab. I create all the link in behind code, and I want that when I click into a link, the content of the usercontrol change in function of the displayname.

Currently, I know how to take the displayname when the it detect that I change source. But I want to have the same Source for all the link, and the content of the usercontrol change in function of the displayname of the selected link.

That's my xaml code of the moderntab :

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

That's is the code behind :

public ListEcoles()
{
    InitializeComponent();
    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) });
}

private void listEcole_SelectedSourceChanged(object sender, SourceEventArgs e)
{
    var selectedLink = listEcole.Links.FirstOrDefault(x => x.Source == listEcole.SelectedSource);
    if (selectedLink != null)
    {
        string selectedDisplayName = selectedLink.DisplayName;
        MessageBox.Show(selectedDisplayName);
    }
}

It doesn't work here because every sources of my link are the same, so the event never proceed.

Can some one help me please.

mm8
  • 163,881
  • 10
  • 57
  • 88

1 Answers1

1

Handle another event like the PreviewMouseLeftButtonUp then. You could use a helper method that finds the ListBoxItem in the visual tree and then get a reference to the corresponding Link object using the ListBoxItem's DataContext property.

Try this:

public ListEcoles()
{
    InitializeComponent();
    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) });
    listEcole.PreviewMouseLeftButtonUp += ListEcole_MouseLeftButtonUp;
}

private void ListEcole_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    ListBoxItem lbi = e.OriginalSource as ListBoxItem;
    if (lbi == null)
    {
        lbi = FindParent<ListBoxItem>(e.OriginalSource as DependencyObject);
    }

    if (lbi != null)
    {
        Link selectedLink = lbi.DataContext as Link;
        if (selectedLink != null)
        {
            string selectedDisplayName = selectedLink.DisplayName;
            MessageBox.Show(selectedDisplayName);
        }
    }
}

private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(dependencyObject);

    if (parent == null) return null;

    var parentT = parent as T;
    return parentT ?? FindParent<T>(parent);
}
mm8
  • 163,881
  • 10
  • 57
  • 88