0
我試圖將TextBlock綁定到TimeSpan,但我需要的格式是這樣的,如果TotalMinutes小於60,它應該顯示「X min」,否則它應該顯示「X h」。有條件的數據綁定到TimeSpan?
它有可能嗎?這可能需要在xaml中進行tom邏輯測試?
我試圖將TextBlock綁定到TimeSpan,但我需要的格式是這樣的,如果TotalMinutes小於60,它應該顯示「X min」,否則它應該顯示「X h」。有條件的數據綁定到TimeSpan?
它有可能嗎?這可能需要在xaml中進行tom邏輯測試?
您應該使用自定義IValueConverter
實現。有幾個教程,例如Data Binding using IValueConverter
in Silverlight。
你IValueConverter
實現應該看起來像:
public class TimeSpanToTextConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is TimeSpan))
throw new ArgumentException("value has to be TimeSpan", "value");
var timespan = (TimeSpan) value;
if (timespan.TotalMinutes > 60)
return string.Format("{0} h", timespan.Hours.ToString());
return string.Format("{0} m", timespan.Minutes.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}