リストボックスには配列によってデータが取り込まれ、その中に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分離コードファイル内のインデックスを与えます。
これを行うにはどうすればよいですか?
おかげ
編集: ここで問題にされているコードです。
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にデータが含まれ、子供につながる(配列)、私は必要なすべての情報を持っているDATA2につながる:
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;
}
はまた、ここに私のオブジェクトのコードです。この点までお手伝いいただきありがとうございます。
こんにちは、応答に感謝です。まず、観測可能なコレクションを使用していませんが、代わりに配列を使用しています。私はデータを解析してWebサイトから取得したので、提供したものと一緒に行きました。基本的に、リストボックスは、いくつかの文字列とboolなどを含むオブジェクトの配列を実行し、これらのオブジェクトのインデックスからデータを取得しています。データを取得する行は次のとおりです。data.children [0] .data.title;またはURL、著者など – Wilcoholic
@Wilcoholic上記の解決策を試しましたか?それは配列で動作するはずです。 –
はい私は上記の解決策を試しても問題は解決しませんでした。コードスニペットのMyObj部分を自分のオブジェクト(RootObject)に置き換え、コード内にOutofBounds例外を取得しました。私は、クリックイベントコードを含むように投稿を更新しました。私はここで何が起こっているのか分からないので、これを間違って実装しようとしたかもしれません。 – Wilcoholic