listview控件和winform下有很大不同。仅仅是怎样得到listview的选定项,我就摸索了三天时间。现在问题终于解决,现在总结如下:
这个例子是对listview的项进行了分组。MSDN给出了对listview分组的例子。数据源是xml。我们还用这个例子。
xaml中代码如下:

Code
1
<Window x:Class="listviewapp.Window1"
2
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
Title="Window1" Height="300" Width="300">
5
<Window.Resources>
6
<XmlDataProvider x:Key="MyData" XPath="/Info">
7
<x:XData>
8
<Info xmlns="">
9
<Item ID="12345" Name="Book 1" Price="$32.05"
10
Author="Author A" Catalog="Business"/>
11
<Item ID="13590" Name="Book 2" Price="$10.00"
12
Author="Author B" Catalog="Language"/>
13
<Item ID="24678" Name="Book 3" Price="$9.00"
14
Author="Author C" Catalog="Language"/>
15
<Item ID="65432" Name="Book 4" Price="$8.50"
16
Author="Author D" Catalog="Business"/>
17
<Item ID="11233" Name="Book 5" Price="$19.00"
18
Author="Author E" Catalog="Health"/>
19
<Item ID="94837" Name="Book 6" Price="$8.50"
20
Author="Author F" Catalog="Language"/>
21
</Info>
22
</x:XData>
23
</XmlDataProvider>
24
<!--<SnippetGroupingCollectionViewSource>-->
25
<CollectionViewSource x:Key='src'
26
Source="{Binding Source={StaticResource MyData},
27
XPath=Item}">
28
<CollectionViewSource.GroupDescriptions>
29
<PropertyGroupDescription PropertyName="@Catalog" />
30
</CollectionViewSource.GroupDescriptions>
31
</CollectionViewSource>
32
<!--</SnippetGroupingCollectionViewSource>-->
33
</Window.Resources>
34
35
<!--<SnippetListViewGroups>-->
36
<ListView Name="listViewTask" ItemsSource='{Binding Source={StaticResource src}}'
37
BorderThickness="0" SelectionChanged="listViewTask_SelectionChanged" >
38
<ListView.GroupStyle>
39
<GroupStyle>
40
<GroupStyle.ContainerStyle>
41
<Style TargetType="{x:Type GroupItem}">
42
<Setter Property="Margin" Value="0,0,0,5"/>
43
<Setter Property="Template">
44
<Setter.Value>
45
<ControlTemplate TargetType="{x:Type GroupItem}">
46
<Expander IsExpanded="True" BorderBrush="#FFA4B97F"
47
BorderThickness="0,0,0,1">
48
<Expander.Header>
49
<DockPanel>
50
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}"
51
Margin="5,0,0,0" Width="100"/>
52
<TextBlock FontWeight="Bold"
53
Text="{Binding Path=ItemCount}"/>
54
</DockPanel>
55
</Expander.Header>
56
<Expander.Content>
57
<ItemsPresenter />
58
</Expander.Content>
59
</Expander>
60
</ControlTemplate>
61
</Setter.Value>
62
</Setter>
63
</Style>
64
</GroupStyle.ContainerStyle>
65
</GroupStyle>
66
</ListView.GroupStyle>
67
<!--</SnippetListViewGroups>-->
68
<ListView.View>
69
<GridView>
70
<GridViewColumn Header="ID"
71
DisplayMemberBinding="{Binding XPath=@ID}"
72
Width="100" />
73
<GridViewColumn Header="Name"
74
DisplayMemberBinding="{Binding XPath=@Name}"
75
Width="140" />
76
<GridViewColumn Header="Price"
77
DisplayMemberBinding="{Binding XPath=@Price}"
78
Width="80" />
79
<GridViewColumn Header="Author"
80
DisplayMemberBinding="{Binding XPath=@Author}"
81
Width="80" />
82
</GridView>
83
</ListView.View>
84
<!--<SnippetListViewEnd>-->
85
</ListView>
86
<!--</SnippetListViewEnd>-->
87
</Window>
88
对后台生成的listViewTask_SelectionChanged事件加入如下代码:

Code
1
private void listViewTask_SelectionChanged(object sender, SelectionChangedEventArgs e)
2
{
3
XmlElement mySelectedElement = (XmlElement)listViewTask.SelectedItem;
4
string xf = mySelectedElement.GetAttribute("Name").ToString()
5
+ " by " + mySelectedElement.GetAttribute("Author").ToString();
6
MessageBox.Show(xf);
7
}运行结果如下:

如果数据源不是xml。比如是数据库,则可以修改如下:

Code
1
private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
2
{
3
DataRowView mySelectedElement =
4
(DataRowView)listView.SelectedItem;
5
string fx = mySelectedElement.Row[1].ToString();
6
//+ " by " + mySelectedElement.GetAttribute("Artist").ToString();
7
8
MessageBox.Show(fx);
9
}代码下载:代码
这个例子是对listview的项进行了分组。MSDN给出了对listview分组的例子。数据源是xml。我们还用这个例子。
xaml中代码如下:
1
<Window x:Class="listviewapp.Window1"2
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"4
Title="Window1" Height="300" Width="300">5
<Window.Resources>6
<XmlDataProvider x:Key="MyData" XPath="/Info">7
<x:XData>8
<Info xmlns="">9
<Item ID="12345" Name="Book 1" Price="$32.05" 10
Author="Author A" Catalog="Business"/>11
<Item ID="13590" Name="Book 2" Price="$10.00" 12
Author="Author B" Catalog="Language"/>13
<Item ID="24678" Name="Book 3" Price="$9.00" 14
Author="Author C" Catalog="Language"/>15
<Item ID="65432" Name="Book 4" Price="$8.50" 16
Author="Author D" Catalog="Business"/>17
<Item ID="11233" Name="Book 5" Price="$19.00" 18
Author="Author E" Catalog="Health"/>19
<Item ID="94837" Name="Book 6" Price="$8.50" 20
Author="Author F" Catalog="Language"/>21
</Info>22
</x:XData>23
</XmlDataProvider>24
<!--<SnippetGroupingCollectionViewSource>-->25
<CollectionViewSource x:Key='src' 26
Source="{Binding Source={StaticResource MyData}, 27
XPath=Item}">28
<CollectionViewSource.GroupDescriptions>29
<PropertyGroupDescription PropertyName="@Catalog" />30
</CollectionViewSource.GroupDescriptions>31
</CollectionViewSource>32
<!--</SnippetGroupingCollectionViewSource>-->33
</Window.Resources>34

35
<!--<SnippetListViewGroups>-->36
<ListView Name="listViewTask" ItemsSource='{Binding Source={StaticResource src}}' 37
BorderThickness="0" SelectionChanged="listViewTask_SelectionChanged" >38
<ListView.GroupStyle>39
<GroupStyle>40
<GroupStyle.ContainerStyle>41
<Style TargetType="{x:Type GroupItem}">42
<Setter Property="Margin" Value="0,0,0,5"/>43
<Setter Property="Template">44
<Setter.Value>45
<ControlTemplate TargetType="{x:Type GroupItem}">46
<Expander IsExpanded="True" BorderBrush="#FFA4B97F" 47
BorderThickness="0,0,0,1">48
<Expander.Header>49
<DockPanel>50
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" 51
Margin="5,0,0,0" Width="100"/>52
<TextBlock FontWeight="Bold" 53
Text="{Binding Path=ItemCount}"/>54
</DockPanel>55
</Expander.Header>56
<Expander.Content>57
<ItemsPresenter />58
</Expander.Content>59
</Expander>60
</ControlTemplate>61
</Setter.Value>62
</Setter>63
</Style>64
</GroupStyle.ContainerStyle>65
</GroupStyle>66
</ListView.GroupStyle>67
<!--</SnippetListViewGroups>-->68
<ListView.View>69
<GridView>70
<GridViewColumn Header="ID" 71
DisplayMemberBinding="{Binding XPath=@ID}" 72
Width="100" />73
<GridViewColumn Header="Name" 74
DisplayMemberBinding="{Binding XPath=@Name}" 75
Width="140" />76
<GridViewColumn Header="Price" 77
DisplayMemberBinding="{Binding XPath=@Price}"78
Width="80" />79
<GridViewColumn Header="Author" 80
DisplayMemberBinding="{Binding XPath=@Author}" 81
Width="80" />82
</GridView>83
</ListView.View>84
<!--<SnippetListViewEnd>-->85
</ListView>86
<!--</SnippetListViewEnd>-->87
</Window>88

1
private void listViewTask_SelectionChanged(object sender, SelectionChangedEventArgs e)2

{3
XmlElement mySelectedElement = (XmlElement)listViewTask.SelectedItem;4
string xf = mySelectedElement.GetAttribute("Name").ToString()5
+ " by " + mySelectedElement.GetAttribute("Author").ToString();6
MessageBox.Show(xf);7
}
如果数据源不是xml。比如是数据库,则可以修改如下:
1
private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)2

{3
DataRowView mySelectedElement =4
(DataRowView)listView.SelectedItem;5
string fx = mySelectedElement.Row[1].ToString();6
//+ " by " + mySelectedElement.GetAttribute("Artist").ToString(); 7
8
MessageBox.Show(fx);9
}
浙公网安备 33010602011771号