Note that there are some explanatory texts on larger screens.

plurals
  1. PODateTime Converter in Autogenerated Datagrid
    text
    copied!<p>I have created an autogenerated datagrid with 5 columns, 3 of which are DateTime and the other 2 are Strings. I need to be able to remove the time from the end of the datetime columns entries.</p> <p>Normally i use a dateconverter but i am getting strange results from it, i'm guessing this is because its applied on the whole datagrid and not just the datetime columns.</p> <p>Can anyone help me fix this? is there a way to single out the datetime columns to apply the converter?</p> <p>Thanks and i'll attach some code below:</p> <p>MainPage.xaml:</p> <pre><code> &lt;UserControl x:Class="Peppermint.Flix.UI.Views.MainPage" xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" xmlns:local="clr-namespace:Peppermint.Flix.UI" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"&gt; &lt;UserControl.Resources&gt; &lt;local:DateTimeConverter x:Key="DateTimeConverter" /&gt; &lt;/UserControl.Resources&gt; &lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="0.5*" /&gt; &lt;RowDefinition Height="10*" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid x:Name="QuickNav" Grid.Row="0"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="0.2*"/&gt; &lt;ColumnDefinition Width="1*"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid x:Name="ComboBox" Grid.Column="0"&gt; &lt;ComboBox HorizontalAlignment="Stretch" Height="20" ItemsSource="{Binding NavItems}" SelectedItem="{Binding NavItem, Mode=TwoWay}" /&gt; &lt;/Grid&gt; &lt;Grid x:Name="GoButton" Grid.Column="1"&gt; &lt;Button Content="Go" HorizontalAlignment="Left" Height="20" Command="{Binding GoCommand, Mode=OneTime}" /&gt; &lt;/Grid&gt; &lt;/Grid&gt; &lt;Grid x:Name="DataGrid" Grid.Row="1" &gt; &lt;sdk:DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="True" ItemsSource="{Binding TransferPackages, Converter={StaticResource DateTimeConverter} }"/&gt; &lt;/Grid&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>MainViewModel.cs:</p> <pre><code> public class MainViewModel : ViewModelBase { private string _navItem; private TransferPackageViewModel _data; #region Constructor public MainViewModel() { GoCommand = new DelegateCommand&lt;object&gt;(QuickNavGo); TransferPackages = new ObservableCollection&lt;TransferPackageViewModel&gt;(); NavItems = new List&lt;string&gt; { "QUICK NAV", "New Transfer Package" }; TransferPackages.Add(new TransferPackageViewModel { TransferPackageName = "Harry Potter 7 The Golden Amulet in 4D (12A)", CreatedBy = "Bilbo Baggins" }); TransferPackages.Add(new TransferPackageViewModel { TransferPackageName = "Harry Potter 8 The Hairy InnKeeper in 4D (12A)", CreatedBy = "Bilbo Baggins" }); TransferPackages.Add(new TransferPackageViewModel { TransferPackageName = "Harry Potter 7 The Milking the Cow in 5D (12A)", CreatedBy = "Bilbo Baggins" }); } #endregion public TransferPackageViewModel Data { get { return _data; } set { _data = value; OnPropertyChanged("Data"); } } public void QuickNavGo(object obj) { MessageBox.Show("This will open the new Transfer Package Window"); } public string NavItem { get { return _navItem; } set { _navItem = value; OnPropertyChanged("NavItem"); } } public ObservableCollection&lt;TransferPackageViewModel&gt; TransferPackages { get; set; } public List&lt;string&gt; NavItems { get; set; } public ICommand GoCommand { get; set; } } } </code></pre> <p>DateTimeConverter.cs:</p> <pre><code> public class DateTimeConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (parameter != null) { string formatString = parameter.ToString(); if (!string.IsNullOrEmpty(formatString)) { return string.Format(culture, formatString, value); } } return value.ToString(); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null) { return DateTime.Parse(value.ToString()); } return value; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload