查看:
<TextBlock Text="{Binding Date}"/>
我想将日期格式化为“dd/MM/yyyy”,换句话说,不包括时间。
我尝试过这样做:<TextBlock Text="{Binding Date, StringFormat={}{0:dd/MM/yyyy}}"/>
,但它不起作用。
给我一个错误:在类型“Binding”中未找到属性“StringFormat”。
查看:
<TextBlock Text="{Binding Date}"/>
我想将日期格式化为“dd/MM/yyyy”,换句话说,不包括时间。
我尝试过这样做:<TextBlock Text="{Binding Date, StringFormat={}{0:dd/MM/yyyy}}"/>
,但它不起作用。
给我一个错误:在类型“Binding”中未找到属性“StringFormat”。
最好、最简单的方式是使用转换器来传递日期,并获得格式化后的字符串。例如,在MyNamespace.Converters
命名空间中:
public class DateFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
return null;
DateTime dt = DateTime.Parse(value.ToString());
return dt.ToString("dd/MM/yyyy");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
在你的XAML中引用转换器,并添加以下转换器:
xmlns:conv="using:MyNamespace.Converters"
<conv:DateFormatConverter x:Name="DateToStringFormatConverter"/>
<TextBlock Text="{Binding Date, Converter={StaticResource DateToStringFormatConverter}"/>
我知道现在可能有点晚了,但是我也有同样的问题,并且想出了这个解决方案。虽然不一定是最简洁的方法,但它是纯XAML。
<TextBlock>
<Run Text="{x:Bind Foo.StartDate.Day}"/>.<Run Text="{x:Bind Foo.StartDate.Month}"/>.<Run Text="{x:Bind Foo.StartDate.Year}"/>
</TextBlock>
StringFormat
的属性。您可以使用Converter和ConverterParameter来实现此功能。您可以参考格式化或转换数据值以供显示。DatePicker
日期到TextBlock
文本的示例:<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.Resources>
<local:DateFormatter x:Key="DateConverter"/>
</Grid.Resources>
<DatePicker Name="ConverterParmeterCalendarViewDayItem"></DatePicker>
<TextBlock Height="100" VerticalAlignment="Top" Text="{Binding ElementName=ConverterParmeterCalendarViewDayItem, Path=Date, Converter={StaticResource DateConverter},ConverterParameter=\{0:dd/MM/yyyy\}}" />
</Grid>
在代码后台,使用DateFormatter类:
public class DateFormatter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var a = language;
// Retrieve the format string and use it to format the value.
string formatString = parameter as string;
if (!string.IsNullOrEmpty(formatString))
{
return string.Format(formatString, value);
}
return value.ToString();
}
// No need to implement converting back on a one-way binding
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return DependencyProperty.UnsetValue;
}
}
xmlns:local="using:'your project'"
吗?如果没有,XAML设计工具可能会出现问题,它可能被标记为错误,但你仍然可以运行这个项目,它应该能正常工作。 - Grace FengText="{x:Bind sys:String.Format('{0:dd/MM/yyyy}', ViewModel.Date)}"
确保您已经包含了对System命名空间的引用:
<Page
xmlns:sys="using:System"
...
{x:Bind ViewModel.PropertyWithDateTime.ToString("....")}
这里有一个很好的例子:
如果您的转换器类在不同的命名空间中(建议放在单独的文件夹中),您可以添加:
xmlns:conv="using:MyNamespace.Converters"
<UserControl.Resources>
<conv:DateToStringConverter x:Key="Converter1"/>
</UserControl.Resources>
StringFormat
的好处在于它允许您指定输出的格式。这是我使用的一个转换器,它允许您指定格式。
public sealed class DateTimeToStringConverter : IValueConverter
{
public static readonly DependencyProperty FormatProperty =
DependencyProperty.Register(nameof(Format), typeof(bool), typeof(DateTimeToStringConverter), new PropertyMetadata("G"));
public string Format { get; set; }
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is DateTime dateTime && value != null)
{
return dateTime.ToString(Format);
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return DateTime.ParseExact(value.ToString(), Format, CultureInfo.CurrentCulture);
}
}
如何使用(带有多种格式的示例):
<Page.Resources>
<ResourceDictionary>
<converters:DateTimeToStringConverter
x:Name="dateStringConverter"
Format="dd-MM-yyyy" />
<converters:DateTimeToStringConverter
x:Name="timeStringConverter"
Format="HH:mm" />
</ResourceDictionary>
</Page.Resources>
<!-- Display the date -->
<TextBlock Text="{Binding Path=Date, Converter={StaticResource dateStringConverter}}" />
<!-- Display the time -->
<TextBlock Text="{Binding Path=Date, Converter={StaticResource timeStringConverter}}" />
试一下这个,
<Label x:Name="LblEstEndTime" Text="{Binding EndTime, StringFormat='Est. End Time: {0:MM/dd/yy h:mm tt}'}" Style="{StaticResource ListCellSubTitleStyle}" VerticalOptions="EndAndExpand" />
你可以在xml本身中完成这个操作。在这里...
<DatePicker
SelectedDate="{Binding Date, Mode=TwoWay}"
Text="{Binding ., StringFormat='dd/MM/yyyy'}"/>
Binding
类中,不存在StringFormat
。此外,XML与XAML并不相同。虽然XAML是基于XML的技术,但XAML和XML并不是可互换的术语。 - The Pademelon