I am making simple RSS reader for Windows Phone which reads an XML file using XML Serializer and displays the list of items. I have Rss.css file and amongst others I have item class (below fragment):
public class Item
{
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("link")]
public string Link { get; set; }
}
And I am binding data in XAML files and displaying eg title field like this:
<ListView Grid.Row="1" ItemsSource="{Binding Rss.Channel.Items}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}"/>
etc., and it's working fine. Now, let's say that in the XML title has an attribute, eg short="true". How do I bind and display this attribute?
I tried to create another class under Item class:
public class Title
{
[XmlAttribute("short")]
public string Short { get; set; }
}
and simply bind the attribute like this:
<TextBlock Text="{Binding Title.Short}"/>
but it's not working. Can I "reach" it in XAML somehow or should I change something in the .cs file?
PS. The given example is a shorter alternative to my problem therefore it is not necessarily very logical.
You're binding to something that doesn't exist - Title
is a string
in your model. You should change this so the deserialization can give you both the title and the attribute:
public class Item
{
[XmlElement("title")]
public Title Title { get; set; }
[XmlElement("link")]
public string Link { get; set; }
}
public class Title
{
[XmlAttribute("short")]
public string Short { get; set; }
[XmlText]
public string Value { get; set; }
}
Then your current Title
binding changes to Title.Value
and your Title.Short
binding should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.