从IList获取当前选定的行

时间:2012-02-22 00:07:35

标签: wpf

我的XAML文件:

<Grid>
        <DataGrid x:Name="LibraryView" ItemsSource="{Binding Path=Elements[track]}"  SelectionChanged="LoadAlbumDetails" SelectionMode="Single">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Artist" Binding="{Binding Path=Element[artist_name].Value}"  />
                <DataGridTextColumn Header="Album" Binding="{Binding Path=Element[album_name].Value}"/>
                <DataGridTextColumn Header="Length" Binding="{Binding Path=Element[duration].Value}"/>
                <DataGridTextColumn Header="Price" Binding="{Binding Path=Element[price].Value}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

我的XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<albums>
    <track>
        <id>211</id>
        <name>If you say something..</name>
        <duration>156</duration>
        <artist_id>13</artist_id>
        <artist_name>Richard Keating</artist_name>
        <album_id>29</album_id>
        <album_name>Don't say anything..</album_name>
        <price>$10</price>
   </track>

   <track>
        <id>212</id>
        <name>My heart is a stereo</name>
        <duration>150</duration>
        <artist_id>14</artist_id>
        <artist_name>Maroon 5</artist_name>
        <album_id>30</album_id>
        <album_name>Maroon 5 stereo</album_name>
        <price>$15</price>
   </track>
</albums>

在我的xaml.cs文件中

  protected void LoadAlbumDetails(object sender, SelectionChangedEventArgs e)
  {
    IList rows = LibraryView.SelectedItems;

  }

如何从所选行中获取名称(专辑名称)?

?rows[0].ToString()
"<track>\r\n  <id>212</id>\r\n  <name>My heart is a stereo</name>\r\n  <duration>150</duration>\r\n  <artist_id>14</artist_id>\r\n  <artist_name>Maroon 5</artist_name>\r\n  <album_id>30</album_id>\r\n  <album_name>Maroon 5 stereo</album_name>\r\n  <price>$15</price>\r\n</track>"

我想单独取出相册名称?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

将项目转换为其类型,我认为这里将是XElement或类似的东西。 e.g。

XElement row = (XElement)rows[0];
// <do something with row, access values etc.>
row.Element("name").Value;