[英]WPF Listbox control
I am new to WPF.I want to get the text of the TextBlock Control corresponding to the particular Listitem which is selected on its UserList_SelectionChanged event. 我是WPF的新手,我想获取与在UserList_SelectionChanged事件中选择的特定Listitem对应的TextBlock控件的文本。
<ListView Height="188" Canvas.Left="11" Canvas.Top="167" Width="252">
<ListBox Name="UserList" Width="224" BorderThickness="0" SelectionChanged="UserList_SelectionChanged_1">
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<Image Source="Status-user-online-icon.png" Height="19" Width="18" />
<TextBlock FontSize="14" FontStretch="Expanded" Width="98">User1</TextBlock>
<Line Stroke="red" X1="0" Y1="25" X2="{Binding ElementName=root, Path=Width}" Y2="25" Opacity="0.22" />
</StackPanel>
</ListBoxItem>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<Image Source="Status-user-online-icon.png" Height="19" Width="18" />
<TextBlock FontSize="14" FontStretch="Expanded" Width="98">User2</TextBlock>
<Line Stroke="red" X1="0" Y1="25" X2="{Binding ElementName=root, Path=Width}" Y2="25" />
</StackPanel>
</ListBoxItem>
</ListBox>
</ListView>
Any help would be appreciated. 任何帮助,将不胜感激。
Please go through the below example 请通过下面的例子
Xaml - Xaml-
<Grid>
<ListView Name="lstView" Margin="10" SelectionChanged="lstView_SelectionChanged">
<ListViewItem>
<StackPanel Orientation="Horizontal">
<TextBlock>One</TextBlock>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel Orientation="Horizontal">
<TextBlock>Two</TextBlock>
</StackPanel>
</ListViewItem>
<ListViewItem>
<StackPanel Orientation="Horizontal">
<TextBlock>Three</TextBlock>
</StackPanel>
</ListViewItem>
</ListView>
</Grid>
In code behind 在后面的代码中
private void lstView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
object selectedEntry = lstView.SelectedItem;
ListBoxItem lbi = (ListBoxIte)lstView.SelectedItem;
var stackPanel = lbi.Content as StackPanel;
foreach (var child in stackPanel.Children)
{
MessageBox.Show((child as TextBlock).Text);
}
}
I cannot see the point of puting ListBox inside ListView, reflect on that. 我看不到将ListBox放入ListView的意义,对此进行反思。 Here you have your TextBlock
在这里,您有您的TextBlock
((StackPanel)((ListBoxItem)UserList.SelectedItem).Content).Children.OfType<TextBlock>().FirstOrDefault();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.