2017-03-24 5 views
0

私のプロジェクトにいくつかの問題が発生しました。私はデータベースへのリクエストによってリンクを埋めるmoderntabを持っています。私の要求の結果は、私のmoderntabのリンクの表示名になる文字列のリストを返します。私は背後のコードですべてのリンクを作成します。リンクにクリックすると、displaycontrolのusercontrolの内容が変化します。ユーザコントロールの内容をmui Link(WPF)から変更してください

現在、私はsourceを変更したことを検出したときにdisplaynameを取得する方法を知っています。しかし、私はすべてのリンクのための同じソースを持っていたい、そして選択されたリンクのdisplaynameの関数でusercontrol変更の内容。

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

です背後にあるコードです:

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

それは私のリンクのすべてのソースが同じであるため、ここで働い、そうしないmoderntabの私のXAMLコードだ

イベントは進まない。

お願いします。

答えて

0

のような別のイベントを処理します。ビジュアルツリーでListBoxItemを見つけ、ListBoxItemDataContextプロパティを使用して、対応するLinkオブジェクトへの参照を取得するヘルパーメソッドを使用できます。

これを試してみてください:

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); 
} 
+0

はあなたの助けをたくさんありがとう、それは私がやってみたかったまさにそれです! –

関連する問題