wpf中得到listview的选定项

listview控件和winform下有很大不同。仅仅是怎样得到listview的选定项,我就摸索了三天时间。现在问题终于解决,现在总结如下:
这个例子是对listview的项进行了分组。MSDN给出了对listview分组的例子。数据源是xml。我们还用这个例子。
xaml中代码如下:
ContractedBlock.gif ExpandedBlockStart.gif 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事件加入如下代码:
ContractedBlock.gif ExpandedBlockStart.gif Code
1private void listViewTask_SelectionChanged(object sender, SelectionChangedEventArgs e)
2ExpandedBlockStart.gifContractedBlock.gif        {
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。比如是数据库,则可以修改如下:
ContractedBlock.gif ExpandedBlockStart.gif Code
1private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
2ExpandedBlockStart.gifContractedBlock.gif        {
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        }
代码下载: 代码

转载于:https://www.cnblogs.com/liuyazhou/archive/2009/09/10/1563886.html

WPF,通过点击ListView的不同实现动态切换窗口布局,可以通过以下步骤完成: 1. 创建一个MainWindow窗口,并在该窗口上放置一个ListView组件和一个ContentControl组件。 2. 在MainWindow的XAML代码,为ListView的ItemsSource属性绑定一个ObservableCollection对象,该对象存储着不同的数据。 3. 在MainWindow的代码,为ListView的SelectionChanged事件注册一个事件处理程序。该事件处理程序用于控制ContentControl的内容。 4. 在SelectionChanged事件处理程序,获取ListView的数据,并根据选,动态切换窗口布局。 5. 可以根据需要创建多个不同布局的UserControl,并在MainWindow为每个布局创建一个实例。 6. 在SelectionChanged事件处理程序,根据选的数据,展示对应的布局。 示例代码如下: MainWindow.xamlListView和ContentControl部分: ```xaml <ListView x:Name="listView" SelectionChanged="ListView_SelectionChanged"> <ListView.ItemTemplate> <DataTemplate> <!-- 设置ListView模板 --> </DataTemplate> </ListView.ItemTemplate> </ListView> <ContentControl x:Name="contentControl"/> ``` MainWindow.xaml.cs的事件处理程序: ```csharp private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e) { // 获取选的数据 var selectedItem = listView.SelectedItem; // 根据选的数据,动态切换窗口布局 if (selectedItem == "布局1") { contentControl.Content = new Layout1UserControl(); } else if (selectedItem == "布局2") { contentControl.Content = new Layout2UserControl(); } // 根据需要添加更多布局的切换规则 } ``` 通过以上步骤,就可以实现通过点击ListView的不同动态切换窗口布局。当用户选择不同时,ContentControl会根据选显示对应的布局。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值