wpf 如何在wpf listview中获取所选行的列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30274071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the column value of the selected row in wpf listview
提问by YWah
In my listview, I've three columns, the first column is displayed as text with image and the rest of the columns just text only. The listview is coded as below:
在我的列表视图中,我有三列,第一列显示为带有图像的文本,其余列仅显示为文本。列表视图的编码如下:
<TabItem x:Name="HistoryTab" Header="History" Style="{StaticResource TabStyle}">
<Grid>
<ListView x:Name="HistoryTabLv" HorizontalAlignment="Left" Height="164" Width="275" VerticalAlignment="Top" SelectionChanged="HistoryTabLv_SelectionChanged" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn x:Name="TimeColumn" Header="Time" Width="85">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="-5,0,0,0">
<Image x:Name="Img" Height="12" Width="12" Source="{Binding Image}" Stretch="Uniform"/>
<TextBlock Text="{Binding Time}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="PhoneNumColumn" Header="Phone Number" Width="85" DisplayMemberBinding="{Binding PhoneNum}" />
<GridViewColumn x:Name="DirectionColumn" Header="Direction" Width="95" DisplayMemberBinding="{Binding Direction}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</TabItem>
If the action statement is true, the relevant data will be binded to each column as coded below.
如果操作语句为真,相关数据将被绑定到每一列,代码如下。
private void HistoryTabLv_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (myStatement == true)
{
var uri = new Uri(@"/Resources/time.png", UriKind.Relative);
myImg = new BitmapImage(uri);
DateTime myTime = DateTime.Now;
HistoryTabLv.Items.Insert(0, new { Image = myImg, Time = myTime.ToString("hh:mm:ss tt"), PhoneNum = calledNum,
Direction = "Called out" });
}
}
In winform, if I want to get the second column value of the selected row, it is coded like this: (based on what I've searched)
在winform中,如果我想获取所选行的第二列值,它的编码如下:(基于我搜索的内容)
string secondCol = lv.SelectedItems[0].SubItems[1].Text;
I want to get the second column value of the selected row (in my case is the PhoneNum column), how can I do that in WPF. I tried with the code below but it doesn't work. Please help.
我想获取所选行的第二列值(在我的例子中是 PhoneNum 列),我该如何在 WPF 中做到这一点。我尝试使用下面的代码,但它不起作用。请帮忙。
string myText = (string)((DataRowView)HistoryTabLv.SelectedItems[0])["PhoneNum"];
回答by dkozl
In WPF ListViewItemis just a wrapper for your content object and SelectedItem(s)will be of the same type as item in your source collection so normally you would cast HistoryTabLv.SelectedItemto that type but because, as far as I can see, you use anonymous type it makes it a bit more difficult. I think the easiest way is around your problem is to use dynamic
在 WPF 中,ListViewItem它只是内容对象的包装器,并且与SelectedItem(s)源集合中的项目具有相同的类型,因此通常您会强制HistoryTabLv.SelectedItem转换为该类型,但因为据我所知,您使用的是匿名类型,这使它有点更加困难。我认为解决您的问题的最简单方法是使用dynamic
dynamic selectedItem = HistoryTabLv.SelectedItem;
var phoneNum = selectedItem.PhoneNum;
or
或者
dynamic selectedItem = HistoryTabLv.SelectedItems[0];
var phoneNum = selectedItem.PhoneNum;
EDIT
编辑
If you would create class for you item like
如果你想为你的项目创建类
public class MyItemClass {
public string Image { get; set; }
public string Time { get; set; }
public string PhoneNum { get; set; }
public string Direction { get; set; }
}
and create your item like
并创建您的项目,例如
new MyItemClass {
Image = myImg,
Time = myTime.ToString("hh:mm:ss tt"),
PhoneNum = calledNum,
Direction = "Called out"
}
then you could cast SelectedItem(s)to your item class like
然后你可以投射SelectedItem(s)到你的项目类
var selectedItem = (MyItemType)HistoryTabLv.SelectedItem

