wp7绑定期间的日期格式

时间:2011-03-24 05:04:52

标签: c# xaml windows-phone-7

我尝试了以下示例,但没有运气:

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手机格式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>

1 个答案:

答案 0 :(得分:3)

我在转换器方面遇到了一些麻烦,发现一个简单的方法就是在你绑定的类上有另一个属性,称为DisplayDate。

例如,如果你绑定了OilChangedDate,你可以将这个属性添加到你绑定的类中:

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

然后直接绑定此属性而不是日期。