C# 基于语句绑定时更改数据

C# 基于语句绑定时更改数据,c#,xml,windows-phone-8,binding,C#,Xml,Windows Phone 8,Binding,基于语句绑定xml时,更改数据的最佳方法是什么? 例如,如果“方向”为“N”,则为“北”,依此类推 这是我的c#: 这是XML: <Wind> <direction>N</direction> </Wind> N 提前谢谢你 有两种可能的方法: 1。您可以修改LINQ查询以进行转换: listBox3.ItemsSource = from WindInfo in Xmlwater.Descendants("Wind") sele

基于语句绑定xml时,更改数据的最佳方法是什么? 例如,如果“方向”为“N”,则为“北”,依此类推

这是我的c#:

这是XML:

<Wind>
   <direction>N</direction>
</Wind>

N

提前谢谢你

有两种可能的方法:

1。您可以修改LINQ查询以进行转换:

listBox3.ItemsSource = from WindInfo in Xmlwater.Descendants("Wind")
    select new WindDirection
    {
        Direction = MapValue(WindDirection.Element("direction").Value),
    };
2.您可以实现一个
IValueConverter

public class WindDirectionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, 
                          object parameter, CultureInfo culture)
    {
        return MapValue(value.ToString());
    }

    public object ConvertBack(object value, Type targetType, 
                              object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
将此转换器添加到绑定表达式:

<Page.Resources>
    <conv:WindDirectionConverter" x:Key="WindDirectionConverter" />
</Page.Resources>

<TextBlock Text="{Binding Direction, 
                  Converter={StaticResource WindDirectionConverter}}" />

多谢各位。这真的很有帮助!!
<Page.Resources>
    <conv:WindDirectionConverter" x:Key="WindDirectionConverter" />
</Page.Resources>

<TextBlock Text="{Binding Direction, 
                  Converter={StaticResource WindDirectionConverter}}" />
public string MapValue(string original)
{
    if (original == "N")
    {
        return "North";
    }
    // other conversions
    return original;
}