0

我有列表框,我想将数据绑定到它。我有一个日期时间字段。我已将我的数据保存在日期时间字段中,例如。2013 年 1 月 1 日 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);
                }
4

1 回答 1

2

您可以格式化您的 Text 属性:

Text="{Binding Date, StringFormat='dd/MM/yyyy'}"
于 2013-04-01T11:24:29.490 回答