[英]XAML Binding data of an XML attribute and displaying its value
我正在為Windows Phone制作簡單的RSS閱讀器,該閱讀器使用XML序列化器讀取XML文件並顯示項目列表。 我有Rss.css文件,除其他外,我還有項目類(下面的片段):
public class Item
{
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("link")]
public string Link { get; set; }
}
我將數據綁定到XAML文件中並顯示例如標題字段,如下所示:
<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}"/>
等,並且工作正常。 現在,假設XML標題中有一個屬性,例如short =“ true”。 如何綁定和顯示此屬性?
我試圖在Item類下創建另一個類:
public class Title
{
[XmlAttribute("short")]
public string Short { get; set; }
}
並像這樣簡單地綁定屬性:
<TextBlock Text="{Binding Title.Short}"/>
但它不起作用。 我可以以某種方式在XAML中“到達”它還是應該更改.cs文件中的內容?
PS。 給定的示例是我的問題的較短替代方案,因此它不一定是很合邏輯的。
您將綁定到不存在的內容- Title
是模型中的string
。 您應該更改此設置,以便反序列化可以為您提供標題和屬性:
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; }
}
然后,您當前的Title
綁定將更改為Title.Value
並且Title.Short
綁定應該起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.