<TextBlock Text="{Binding Date}"/>
我想要的日期格式爲 「DD/MM/YYYY」,換句話說,沒有時間。
我試過了:<TextBlock Text="{Binding Date, StringFormat={}{0:dd/MM/yyyy}}"/>
,但它不起作用。
給我一個錯誤:在'綁定'類型中找不到屬性'StringFormat'。
<TextBlock Text="{Binding Date}"/>
我想要的日期格式爲 「DD/MM/YYYY」,換句話說,沒有時間。
我試過了:<TextBlock Text="{Binding Date, StringFormat={}{0:dd/MM/yyyy}}"/>
,但它不起作用。
給我一個錯誤:在'綁定'類型中找不到屬性'StringFormat'。
最好的也是最簡單的方法是使用一個轉換器,將Date傳遞給它並獲取格式化的字符串。在例如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"
在您的XAML頁面,並在page.resources添加此
<conv:DateFormatConverter x:Name="DateToStringFormatConverter"/>
<TextBlock Text="{Binding Date, Converter={StaticResource DateToStringFormatConverter}"/>
有在Binding類中沒有名爲StringFormat
的屬性。您可以使用Converter和ConverterParameter來執行此操作。你可以參考Formatting or converting data values for display。
例如,在這裏,我將DatePicker
的日期綁定到TextBlock
的文本。
XAML:
<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類:
來給出完全相同的答案。最好的辦法是使用轉換器,如上所示,除非您將日期更改爲代碼隱藏字符串,並將其格式化。 [日期時間格式信息](https://msdn.microsoft.com/en-us/library/az4se3k1(v = vs.110).aspx) –
不幸的是,它不在這裏工作..類型'本地:DateFormatter'未找到。我錯過了什麼? – developer033
您需要創建一個名爲DateFormatter的類,請參閱我的DateFormatter類。 –
這裏有一個很好的例子:
如果你的轉換器類是在不同的命名空間(建議在單獨的文件夾中) R),您可以添加
xmlns:conv="using:MyNamespace.Converters"
,並使用它像這樣:
<UserControl.Resources>
<conv:DateToStringConverter x:Key="Converter1"/>
</UserControl.Resources>
其餘應保持一樣從鏈接的例子。
爲什麼要複雜化?你可以使用編譯的數據綁定
{x:Bind ViewModel.PropertyWithDateTime.ToString("....")}
我知道這是遲到了,但我有同樣的問題,並提出了這個解決方案。也許不是最短但純粹的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
的好處是,它允許您指定輸出的格式。這裏是我使用的轉換器,可以讓你指定格式。
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" />
雖然代碼只有答案可能會解決原始問題,但一些解釋將有助於理解您使用的方法以及它的工作原理。 –
它不工作。錯誤:資源「DateStringToFormatConverter」無法解析。 – developer033
您必須在xaml中聲明轉換器才能使用它 – CodeNoob
如果您可以編輯完整答案的答案,那就太好了。 – developer033