2013-04-27 49 views
2

我需要用時間跨度類型在下面的方式命名SumOfPeriods屬性綁定TextBlock.Text:XAML - 結合路徑= TimeSpan.TotalMinutes

<TextBlock> 
    <TextBlock.Text> 
     <MultiBinding StringFormat="{}{0:D1} h {1:D1} min ({2:D1} min)"> 
      <Binding Path="SumOfPeriods.Hours" /> 
      <Binding Path="SumOfPeriods.Minutes" /> 
      <Binding Path="SumOfPeriods.TotalMinutes" /> 
     </MultiBinding> 
    </TextBlock.Text> 
</TextBlock> 

...但它不工作。內容不顯示。 當我刪除綁定到它的工作原理和顯示內容TotalMinutes:

<TextBlock> 
    <TextBlock.Text> 
     <MultiBinding StringFormat="{}{0:D1} h {1:D1} min"> 
      <Binding Path="SumOfPeriods.Hours" /> 
      <Binding Path="SumOfPeriods.Minutes" /> 
     </MultiBinding> 
    </TextBlock.Text> 
</TextBlock> 

任何人都可以幫我嗎?

回答

3

TotalMinutesDouble,但D format specifier僅支持整數類型,如Int32。格式字符串如{}{0:D1} h {1:D1} min ({2} min)應該可以工作。

0

A轉換器可以解決你的問題:

public class PeriodConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     var timeSpan = (TimeSpan)value; 

     // add your format here 
     var text = string.Format("", timeSpan); 

     return text; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return Binding.DoNothing; 
    } 
} 

轉換器添加到您的資源:

<FrameworkElement.Resources> 
    <converters:PeriodConverter x:Key="periodConverter" /> 
</FrameworkElement.Resources> 

更新您的TextBlock的綁定:

<TextBlock Text={Binding Path=SumOfPeriods, Converter={StaticResource periodConverter}} />