将日期时间数据绑定到窗口电话7中的列表框

时间:2013-04-01 11:15:48

标签: windows-phone-7

我有列表框,我想将数据绑定到它。我有一个日期时间字段。我已将日期时间字段中的数据保存为例如。 01/01/2013 12.00.00。现在当我将数据绑定到列表框时,它显示为我保存但我想显示为仅01/01/2013。

XAML代码:

   <Grid x:Name="ContentPanel" >
                <ListBox x:Name="listExpense" SelectionChanged="listExpense_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate >
                            <!--<Button x:Name="btndetails" Width="460" Height="65"  BorderThickness="1" Margin="0,-20,0,0" Click="btndetails_click">
                        <Button.Content>-->
                            <StackPanel  Orientation="Vertical">
                                <StackPanel.Background>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="White" Offset="0"/>
                                    </LinearGradientBrush>
                                </StackPanel.Background>
                                <Border  BorderBrush="#120221" Background="Transparent" BorderThickness="6" >
                                    <StackPanel Orientation="Horizontal">

                                        <TextBlock Width="200" Foreground="Black" FontSize="22" Margin="10,0,0,0" Text="{Binding CategoryName}" Height="30"></TextBlock>
                                        <TextBlock Width="70" Foreground="Black" FontSize="22"  Margin="0,0,0,0" Text="{Binding Price}" Height="30"></TextBlock>
                                        <TextBlock Width="130" Foreground="Black" FontSize="22"  Margin="25,0,50,0"  Text="{Binding Date}" Height="30"></TextBlock>

                                    </StackPanel>
                                </Border>
                            </StackPanel>
                            <!--</Button.Content>
                    </Button>-->
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid

XAMl.cs

 var varExp = from Exp in Empdb.Expense
                             join cat in Empdb.Category
                             on Exp.CategoryId equals cat.CategoryID
                             select new { Exp.ExpenseID, Exp.Date, Exp.Price, Exp.Description, cat.Name };
                foreach (var item in varExp)
                {
                    string[] formats = { "dd/MM/yyyy" };
                    ExpenseVO objExpense = new ExpenseVO();                      

                    string strDate = item.Date.ToString("dd/MM/yyyy");                        
                    objExpense.Date = DateTime.ParseExact(strDate, formats, new CultureInfo("en-US"), DateTimeStyles.None); 
                    objExpense.Price = item.Price;
                    objExpense.CategoryName = item.Name;
                    ExpenseList.Add(objExpense);
                }

1 个答案:

答案 0 :(得分:2)

您可以设置Text属性的格式:

Text="{Binding Date, StringFormat='dd/MM/yyyy'}"