我在组合框Selection Changed Event中动态地将Label和Combo框添加到ListView控件。
我想获取所选列表视图项目的标签内容
答案 0 :(得分:0)
您可以使用VisualTreeHelper从SelectedItem导航Label;
或
简单的方法,你可以像下面这样做(但是在必要时添加空检查);
XAML代码:
<StackPanel>
<ListView SelectionChanged="Selector_OnSelectionChanged">
<ListViewItem>
<StackPanel>
<Label Content="Label1"/>
<ComboBox>
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
</ComboBox>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel>
<Label Content="Label2"/>
<ComboBox>
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
</ComboBox>
</StackPanel>
</ListViewItem>
</ListView>
<TextBlock x:Name="TextBlock1" FontSize="25"/>
</StackPanel>
C#代码:
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
string content =
((((e.AddedItems[0] as ListViewItem).Content) as StackPanel).Children[0] as
Label).Content.ToString();
TextBlock1.Text = "Selected Item's Label content is :" + content;
}
对于简单的解决方案,我已经以编程方式导航;
(((e.AddedItems [0] as ListViewItem).Content)as StackPanel).Children [0] as Label).Content