0

For some reason using Content="{Binding Time, StringFormat=t} is still giving me a long date. The backing field is a DateTime property initialised with DateTime.Now but no matter what string format I try it still shows the full date...

I would like only to see HH:mm tt

Any ideas?

XAML :

<Window x:Class="ArgosSystem.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:loc="clr-namespace:ArgosSystem" xmlns:sys="clr-namespace:System;assembly=System" Title="MainWindow" Height="800" Width="1280" Loaded="Window_Loaded"> <Window.Resources> <DataTemplate DataType="{x:Type loc:Picknote}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="200" MinWidth="200" /> <ColumnDefinition Width="350" /> <ColumnDefinition Width="250" /> <ColumnDefinition Width="50" /> </Grid.ColumnDefinitions> <Label Content="{Binding Time, StringFormat=t}" VerticalContentAlignment="Center" Foreground="IndianRed" FontSize="36" Grid.Column="0" /> <Label Content="{Binding Customer}" VerticalContentAlignment="Center" Foreground="IndianRed" FontSize="36" Grid.Column="1" /> <Label Content="{Binding PicknoteNo}" VerticalContentAlignment="Center" Foreground="IndianRed" FontSize="36" Grid.Column="2" /> <Label Content="{Binding Qty}" VerticalContentAlignment="Center" Foreground="IndianRed" FontSize="36" Grid.Column="3" /> </Grid> </DataTemplate> </Window.Resources> <Grid Background="Black"> <DockPanel> <ScrollViewer Name="lstPicknoteScroll" VerticalScrollBarVisibility="Auto"> <ItemsControl Name="lstPicknotes" ItemsSource="{Binding}" IsTabStop="False" Foreground="Cornsilk" /> </ScrollViewer> </DockPanel> </Grid> </Window> 

C# :

public partial class MainWindow : Window { ObservableCollection<Picknote> picknotes = new ObservableCollection<Picknote>(); public MainWindow() { InitializeComponent(); lstPicknotes.DataContext = picknotes; } private void Window_Loaded(object sender, RoutedEventArgs e) { picknotes.Add(new Picknote { Time = DateTime.Now, Customer = "REED FOR SPEED", PicknoteNo = "PKN767677", Qty = 100 }); picknotes.Add(new Picknote { Time = DateTime.Now.AddHours(-2), Customer = "F1 AUTOMOTIVE", PicknoteNo = "PKN767677", Qty = 50 }); picknotes.Add(new Picknote { Time = DateTime.Now.AddHours(-1), Customer = "FERGUSENS", PicknoteNo = "PKN767677", Qty = 10 }); } } 

1 Answer 1

4

StringFormat works on properties of type string. The Content property is of type Object so you need to specify the format using ContentStringFormat property of Label control.

<Label Content="{Binding Time}" ContentStringFormat="t" VerticalContentAlignment="Center" Foreground="IndianRed" FontSize="36" Grid.Column="0" /> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.