有兩種可能的方法:
您可以修改您的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;
}
非常感謝你非常。這真的有幫助! – user3147928