2011-03-24 48 views
1

期間我嘗試下面的下面的例子中,沒有運氣:WP7日期格式綁定

http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter%28v=VS.95%29.aspx

我也試圖跟着這個問題,以及:

Formatting a date in XAML on WP7

我不能讓它工作。當我嘗試添加引用,如:

<namespace:dateTimeConverter x:Key="MyDateTimeToStringConverter"/> 

<src:DateConverter x:Key="dateConverter"/> 

我不知道我在做什麼錯。任何幫助表示讚賞!

這裏是我的類代碼:

using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Windows.Data; 
using System.Globalization; 

namespace OilChangeApplication 
{ 

    public class dateTimeConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      DateTime date = (DateTime)value; 
      return date.ToShortDateString(); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      string strValue = value as string; 
      DateTime resultDateTime; 
      if (DateTime.TryParse(strValue, out resultDateTime)) 
      { 
       return resultDateTime; 
      } 
      return DependencyProperty.UnsetValue; 
     } 
    } 

} 

這是我的Windows Phone形式XAML:

<phone:PhoneApplicationPage 
    x:Class="OilChangeApplication.historyInfo" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    SupportedOrientations="Portrait" Orientation="Portrait" 
    mc:Ignorable="d" d:DesignHeight="696" d:DesignWidth="480" 
    shell:SystemTray.IsVisible="True"> 


    <!--LayoutRoot is the root grid where all page content is placed--> 
    <Grid x:Name="LayoutRoot" Background="Transparent"> 
     <Grid.Resources> 

     </Grid.Resources> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 



      <!--TitlePanel contains the name of the application and page title--> 
     <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
      <TextBlock x:Name="ApplicationTitle" Text="Change your Oil Application 2.0" Style="{StaticResource PhoneTextNormalStyle}"/> 
      <TextBlock x:Name="PageTitle" Text="history" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
     </StackPanel> 



     <ListBox Grid.Row="1"> 
      <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 


       <ListBox Height="601" HorizontalAlignment="Left" Margin="-3,2,0,0" Name="lbHistory" VerticalAlignment="Top" Width="460" ItemsSource="{Binding historyItemsCollection}"> 

        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal"> 
           <TextBlock FontFamily="Segoe WP Semibold" FontWeight="Bold" FontSize="30" VerticalAlignment="Top" Margin="20,10">*</TextBlock> 
           <StackPanel> 
            <TextBlock x:Name="ItemText" Text="{Binding VehicleName}" FontSize="{StaticResource PhoneFontSizeLarge}"/> 
            <StackPanel Orientation="Horizontal" 
            HorizontalAlignment="Center" 
            VerticalAlignment="Center"> 
              <!--<TextBlock x:Name="ocDate" Text="{Binding OilChangedDate}"></TextBlock>--> 
             <TextBlock Text="{Binding OilChangedDate, Converter={StaticResource dateTimeConverter},ConverterParameter=\{0:M\}}" /> 
             <TextBlock x:Name="ocOdometer" Text="{Binding OilChangedOdometer}" FontSize="{StaticResource PhoneFontSizeNormal}"/> 
             </StackPanel> 
           </StackPanel> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 

       </ListBox> 
      </Grid> 
     </ListBox> 
    </Grid> 


    <!--Sample code showing usage of ApplicationBar--> 
    <phone:PhoneApplicationPage.ApplicationBar> 
     <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 
      <shell:ApplicationBarIconButton IconUri="/Images/check.png" Text="save" x:Name="btnSave" Click="btnSave_Click"/> 
      <shell:ApplicationBarIconButton IconUri="/Images/cancel.png" Text="cancel" x:Name="btnCancel" Click="btnCancel_Click"/> 
     </shell:ApplicationBar> 
    </phone:PhoneApplicationPage.ApplicationBar> 

</phone:PhoneApplicationPage> 

回答

3

我也有一點麻煩,轉換器,發現了一個簡單的工作around是在你綁定的類上有另一個屬性,這些屬性被稱爲DisplayDate的行。

例如,如果您具有約束力OilChangedDate你可以將此屬性添加到您綁定類:

public string OilChangedDisplayDate 
{ 
    get { return OilChangedDate.ToShortDateString(); } 
} 

則此屬性,而不是直接的日期進行綁定。

+0

謝謝。我對這個轉換器感到如此緊張,以至於這個解決方案對我來說太容易了:) - 謝謝! – webdad3 2011-03-24 11:21:20