WPF: How hard could it be to get a SelectedItemTemplate?
http://weblog.west-wind.com/posts/2007/May/23/WPF-How-hard-could-it-be-to-get-a-SelectedItemTemplate
<Window x:Class="WPFlistboxWithClick.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="Provider1" XPath="Students">
<x:XData>
<Students xmlns="">
<Item Name="wangjp" Age="16"></Item>
<Item Name="wangjp" Age="16"></Item>
<Item Name="wangjp" Age="16"></Item>
<Item Name="wangjp" Age="16"></Item>
<Item Name="wangjp" Age="16"></Item>
<Item Name="wangjp" Age="16"></Item>
<Item Name="wangjp" Age="16"></Item>
<Item Name="wangjp" Age="16"></Item>
</Students>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="259*"/>
<ColumnDefinition Width="258*"/>
</Grid.ColumnDefinitions>
<ListBox Name="ListBox1" ItemsSource="{Binding Source={StaticResource Provider1}, XPath=Item/@Name}" SelectionChanged="ListBox1_SelectionChanged"></ListBox>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="52*"/>
<RowDefinition Height="70*"/>
<RowDefinition Height="88*"/>
<RowDefinition Height="109*"/>
</Grid.RowDefinitions>
<Button Click="RemoveSelected"></Button>
</Grid>
</Grid>
</Window>
读取方法
private void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
// string s = listBox.SelectedItem.ToString();
XmlNode element= listBox.SelectedItem as XmlNode;
Trace.WriteLine(element.Value);
Trace.WriteLine("this is on selectedchanged event");
}

浙公网安备 33010602011771号