确定我单击的链接

本文关键字:链接 单击 | 更新日期: 2023-09-27 18:15:07

这里有一个标记:

<UserControl x:Class="NeoClinic.MAS.ConfigurationsList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mui="http://firstfloorsoftware.com/ModernUI"
             mc:Ignorable="d" 
             x:Name="ConfigControl">
    <Grid Style="{StaticResource ContentRoot}">
        <!-- TODO: set @SelectedSource -->
        <mui:ModernTab x:Name="ModTab" Layout="List"  PreviewMouseLeftButtonUp="ModTab_PreviewMouseLeftButtonUp"> 
            <mui:ModernTab.Links >
                <!-- TODO: set @Source -->
                <mui:Link x:Name="BreedLink" DisplayName="Breeds" Source="/Pages/BreedListV2.xaml" />
                <mui:Link x:Name="SpecieLink" DisplayName="Species" Source="/Pages/SpeciesList.xaml" />
            </mui:ModernTab.Links>
        </mui:ModernTab>
    </Grid>
</UserControl>

则事件:

private void ModTab_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            DependencyObject dep = (DependencyObject)e.OriginalSource;
            // iteratively traverse the visual tree
            while ((dep != null) &&
                    !(dep is ListBoxItem) )
            {
                dep = VisualTreeHelper.GetParent(dep);
            }
            if (dep == null)
                return;
            if (dep is ListBoxItem)
            {
                var x = dep.Equals(SpecieLink); //error here
            }
        }

那么我如何确定我点击了哪个链接以便我可以在单个链接中加载不同的用户控件,比如

if(what I clicked == Breeds)
{
    BreedLink.Source = new Uri("/BreedList.xaml", UriKind.Relative);
}
else if (what I clicked == BreedsDetails)
{
    BreedLink.Source = new Uri("/BreedDetails.xaml", UriKind.Relative);
}

或者是否有其他更简单的方法来做到这一点,比如标记绑定?

确定我单击的链接

您可以查看 OriginalSource ,以查看点击的链接如下:

private void ModTab_PreviewMouseLeftButtonUp(object sender,
                                             MouseButtonEventArgs e)
{
   FrameworkElement link = e.OriginalSource as FrameworkElement;
   if(link != null)
   {
      if(link.Name == "BreedLink")
      {
         ......
      }
      else if (link.Name == "SpecieLink")
      {
         ......
      }
   }
}