Source XML
<?xml version="1.0" ?>
<songs>
<song sname="song1.mp3"/>
<song sname="song2.mp3"/>
<song sname="song3.mp3"/>
<song sname="song4.mp3"/>
</songs>
XAML
<sdk:DataGrid
x:Name="DataGrid1"
Margin="0,60,0,0"
IsReadOnly="T开发者_开发问答rue"
AutoGenerateColumns="True"
VerticalScrollBarVisibility="Visible"
BorderBrush="DarkGreen"
BorderThickness="5"
CanUserSortColumns="True"
CanUserResizeColumns="False"
CanUserReorderColumns="False">
</sdk:DataGrid>
Binding Code
void ParseXMLFile(string dataInXmlFile)
{
List<Song> songs = new List<Song>();
XDocument xmlDoc = XDocument.Parse(dataInXmlFile);
songs = (from r in xmlDoc.Descendants("song")
select new Song
{
name = (string)r.Attribute("sname").Value
}).ToList();
DataGrid1.ItemsSource = songs;
}
When the above code runs - the datagrid is empty.
While debugging i can see the object songs have 4 items.
the issue is it not binding properly - any suggestion appreciated.
You haven't shown us the source of your Song
class.
I suspect you've declared the name
property as internal
. You need to to be public
for it to work in a DataGrid.
Replace your code part
DataGrid1.ItemsSource = songs;
with
DataGrid1.ItemsSource = null;
DataGrid1.ItemsSource = songs;
While it looks stupid, this works for me
精彩评论