2011-12-21 70 views
0

所以我有一个列表框正在被一个数组填充,并有25个条目。在列表框的每一行中,我都有一个“注释”超链接按钮,它具有与列表框不同的功能。所以,由于我没有从技术上选择列表框项目,所以不会返回索引。不管怎么说,这里是代码:从列表框中的超链接获取索引

<ListBox Name="mainListBox" SelectionChanged="mainListBox_SelectionChanged" Width="460" HorizontalAlignment="Center" VerticalAlignment="Stretch"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Margin="5" Orientation="Horizontal" HorizontalAlignment="Left"> 
         <Image Source="{Binding data.thumbnail}" Margin="5" VerticalAlignment="Top" Width="70" /> 
         <StackPanel Orientation="Vertical"> 
          <TextBlock x:Name="TitleInfo" Text="{Binding data.title}" TextWrapping="Wrap" Foreground="DarkSeaGreen" Width="370" /> 
           <TextBlock x:Name="AuthorInfo" Text="{Binding data.author}" FontSize="15" Margin="2" /> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="Score:" Margin="2" FontSize="14" /> 
           <TextBlock x:Name="score" Text="{Binding data.score}" FontSize="14" Margin="2"/> 
           <HyperlinkButton Content="Comments" Click="HyperlinkButton_Click" FontSize="15" x:Name="commentsLink" /> 
          </StackPanel> 
         </StackPanel> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

我想这行:

<HyperlinkButton Content="Comments" Click="HyperlinkButton_Click" FontSize="15" x:Name="commentsLink" /> 

给我的指标在XAML文件后面的代码。

我该如何去做这件事?

感谢

编辑: 下面是正在有问题的代码。

private void HyperlinkButton_Click(object sender, RoutedEventArgs e) 
    { 
     var hb = sender as HyperlinkButton; 
     if (hb != null) 
     { 
      var obj = hb.Tag as RootObject; 
      if (obj != null) 
      { 
       MessageBox.Show(obj.data.children[0].data.title, obj.data.children[0].data.author, MessageBoxButton.OK); 
      } 
     } 
     textBlock1.Text = Global.sUrl; 
    } 

而且,这里是我的对象的代码:(数组)

public class MediaEmbed 
{ 
    public string content { get; set; } 
    public int? width { get; set; } 
    public bool? scrolling { get; set; } 
    public int? height { get; set; } 
} 
public class Oembed 
{ 
    public string provider_url { get; set; } 
    public string description { get; set; } 
    public string title { get; set; } 
    public string url { get; set; } 
    public string author_name { get; set; } 
    public int height { get; set; } 
    public int width { get; set; } 
    public string html { get; set; } 
    public int thumbnail_width { get; set; } 
    public string version { get; set; } 
    public string provider_name { get; set; } 
    public string thumbnail_url { get; set; } 
    public string type { get; set; } 
    public int thumbnail_height { get; set; } 
    public string author_url { get; set; } 
} 
public class Media 
{ 
    public string type { get; set; } 
    public Oembed oembed { get; set; } 
} 
public class Data2 
{ 
    public string domain { get; set; } 
    public MediaEmbed media_embed { get; set; } 
    public object levenshtein { get; set; } 
    public string subreddit { get; set; } 
    public string selftext_html { get; set; } 
    public string selftext { get; set; } 
    public object likes { get; set; } 
    public bool saved { get; set; } 
    public string id { get; set; } 
    public bool clicked { get; set; } 
    public string title { get; set; } 
    public Media media { get; set; } 
    public int score { get; set; } 
    public bool over_18 { get; set; } 
    public bool hidden { get; set; } 
    public string thumbnail { get; set; } 
    public string subreddit_id { get; set; } 
    public string author_flair_css_class { get; set; } 
    public int downs { get; set; } 
    public bool is_self { get; set; } 
    public string permalink { get; set; } 
    public string name { get; set; } 
    public double created { get; set; } 
    public string url { get; set; } 
    public string author_flair_text { get; set; } 
    public string author { get; set; } 
    public double created_utc { get; set; } 
    public int num_comments { get; set; } 
    public int ups { get; set; } 
} 
public class Child 
{ 
    public string kind { get; set; } 
    public Data2 data { get; set; } 
} 
public class Data 
{ 
    public string modhash { get; set; } 
    public Child[] children { get; set; } 
    public string after { get; set; } 
    public object before { get; set; } 
} 
public class RootObject 
{ 
    public string kind { get; set; } 
    public Data data { get; set; } 
} 

RootObject包含数据,从而导致孩子,这会导致数据2拥有所有我想要的信息。非常感谢您的帮助。

回答

1

您可以使用Tag参数。请注意,{Binding}绑定了整个对象本身。

例如,

class Myobj 
{ 
    string param1 { get; set; } 
    string param2 { get; set; } 
} 

ObservableCollection<Myobj> collection; 

如果集合是您的ItemsSource,那么你的DataTemplate内,一{}绑定是指MyObj中的整个实例。

<HyperlinkButton Content="Comments" Click="HyperlinkButton_Click" FontSize="15" x:Name="commentsLink" Tag="{Binding}" /> 

然后在您的点击事件中,只需将发件人投射到超链接按钮,然后获取标签。

... 
var hb = sender as HyperLinkButton; 
if (hb != null) 
    { 
    var obj = hb.Tag as Myobj; 
    if (obj != null) 
    { 

    } 
    } 
... 

查看绑定的这个cheatsheet。这非常有帮助。

http://www.nbdtech.com/Free/WpfBinding.pdf

注意,此实现不使用SelectedIndex - 但它并不需要。由于HyperLinkButton提供了您为其生成ListBoxItem的对象,所以不需要它。

最后,这里是一个样本项目

https://skydrive.live.com/redir.aspx?cid=ef08824b672fb5d8&resid=EF08824B672FB5D8!352&parid=EF08824B672FB5D8!343

+0

嗨,感谢您的答复。首先,我没有使用可观察集合,而是使用数组。我正在解析从网站中提取的数据,所以我只是跟他们提供的内容一起去了。基本上,列表框通过一个包含多个字符串和布尔等的对象数组运行,我从这些对象之一的索引中提取数据。取数据的行是这样的:data.children [0] .data.title;或网址,作者等。 – Wilcoholic 2011-12-21 06:27:08

+0

@Wolcoholic你尝试过上述解决方案吗?它应该与一个数组一起工作。 – 2011-12-21 06:37:14

+0

是的,我试过上述解决方案,并没有解决问题。我将代码片段中的MyObj部分替换为我的对象(RootObject),并在我的代码中得到OutofBounds异常。我更新了帖子以包含点击事件代码。我不确定我是否理解这里100%发生了什么,所以我可能试图错误地实现这一点。 – Wilcoholic 2011-12-21 07:40:18