WPF确定我单击的链接

时间:2014-07-27 00:19:04

标签: c# wpf modern-ui

我在这里得到了一个标记:

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

或者是否有其他更简单的方法来执行此操作,例如标记绑定?

1 个答案:

答案 0 :(得分:2)

您可以查看 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")
      {
         ......
      }
   }
}