[英]StringFormat on Binding
視圖:
<TextBlock Text="{Binding Date}"/>
我希望將日期格式化為“dd / MM / yyyy”,換句話說,沒有時間。
我試了一下: <TextBlock Text="{Binding Date, StringFormat={}{0:dd/MM/yyyy}}"/>
,但它不起作用。
給我一個錯誤:在'Binding'類型中找不到屬性'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執行此操作。 您可以參考格式化或轉換數據值進行顯示 。
例如,在這里,我將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類:
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;
}
}
為什么復雜化? 您可以使用編譯數據綁定
{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>
從14393開始,您可以使用x:Bind中的函數 。
這意味着您可以格式化日期,如:
Text="{x:Bind sys:String.Format('{0:dd/MM/yyyy}', ViewModel.Date)}"
只需確保包含對System命名空間的引用:
<Page
xmlns:sys="using:System"
...
這里有一個很好的例子:
如果您的轉換器類位於不同的命名空間(建議位於單獨的文件夾中),您可以添加
xmlns:conv="using:MyNamespace.Converters"
並像這樣使用它:
<UserControl.Resources>
<conv:DateToStringConverter x:Key="Converter1"/>
</UserControl.Resources>
其余部分應與鏈接中的示例保持一致。
您可以在xml中執行此操作。 這里...
<DatePicker
SelectedDate="{Binding Date, Mode=TwoWay}"
Text="{Binding ., StringFormat='dd/MM/yyyy'}"/>
試試這個,
<Label x:Name="LblEstEndTime" Text="{Binding EndTime, StringFormat='Est. End Time: {0:MM/dd/yy h:mm tt}'}" Style="{StaticResource ListCellSubTitleStyle}" VerticalOptions="EndAndExpand" />
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}}" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.