有两种可能的方法:
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}}" />
在这两种情况下,我都假设您有一个 MapValue 函数为您进行值映射:
public string MapValue(string original)
{
if (original == "N")
{
return "North";
}
// other conversions
return original;
}