Hi,
By the way, you said you made it working by using a third format ("{0:ddd MM/dd/yy hh:mm tt}"), which is exactly the same I gave you : "{0:ddd MM/ dd / yy hh:mm tt}".
You can have a look at this page on how to define a Date Format string : https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx.
You can alos do it in the MainWindow.Loaded event. This is done after the DataGridControl is defined in XAML. The Binding will be realized when needed. Please try it in the DataGridControl sample of the LiveExplorer.
Get more controls, features, updates and technical support with Xceed Toolkit Plus for WPF
-
Instead of managing an non-available AutocreateColumn event, you could register to the DataGridControl.Columns.CollectionChanged event (called for a column added, removed or a reset) and then scan the columns to adjust what you want (width...). ex :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Scan columns here (for columns defined in XAML)
this.ScanColumns( null, _dataGrid.Columns );
_dataGrid.Columns.CollectionChanged += Columns_CollectionChanged;
_dataGrid.ItemsSource = new List<MyData>()
{
new MyData() { OrderID = 101, OrderDate = new DateTime( 2017, 5, 10), ShipRegion = "USA" },
new MyData() { OrderID = 122, OrderDate = new DateTime( 2017, 5, 15), ShipRegion = "Canada" },
new MyData() { OrderID = 133, OrderDate = new DateTime( 2017, 4, 25), ShipRegion = "Mexico" },
};
}
private void Columns_CollectionChanged( object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e )
{
// Scan columns here for columns modified later
this.ScanColumns( e.OldItems as IList, e.NewItems as IList );
}
private void ScanColumns( IList oldColumns, IList newColumns )
{
if( newColumns != null )
{
foreach( var column in newColumns )
{
var c = column as Column;
if( (c != null) && (c.FieldName == "OrderID") )
{
c.Width = 200;
}
}
}
}
}- You can use this :
_dataGrid.Columns[ "OrderDate" ].CellContentStringFormat = "{0:ddd MM/ dd / yy hh:mm tt}";
right after the InitializeComponent(); of the MainWindow. You can try it by modifying the DataGridView.xaml.cs file, which is the sample for the DataGridControl in the Toolkit LiveExplorer. You can find this file here : Xceed.Wpf.Toolkit.LiveExplorer/Samples/DataGrid/Views/DatagridView.xaml.cs.By the way, you said you made it working by using a third format ("{0:ddd MM/dd/yy hh:mm tt}"), which is exactly the same I gave you : "{0:ddd MM/ dd / yy hh:mm tt}".
You can have a look at this page on how to define a Date Format string : https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx.
- Again, in the DataGridControl sample from the Toolkit LiveExplorer, simply setting :
var dataTemplate = new DataTemplate();
var date = new FrameworkElementFactory( typeof( TextBlock ) );
date.SetBinding( TextBlock.TextProperty, new Binding( "." ) { Converter = new DisplayedValueConverter() } );
dataTemplate.VisualTree = date;
_dataGrid.Columns[ "OrderDate" ].CellContentTemplate = dataTemplate;
right after the InitializeComponent(); call from the MainWindow is a good place for it to work. It's a binding, so right after the DataGridControl is defined, you can set it and the binding will be realized when needed.You can alos do it in the MainWindow.Loaded event. This is done after the DataGridControl is defined in XAML. The Binding will be realized when needed. Please try it in the DataGridControl sample of the LiveExplorer.
-
The DisplayedValueConverter is not a property, but a Converter defined earlier in this thread. The goal is to use it inside the Binding, through the Binding.Converter property, to convert the bound value to a DateTime.
<Window x:Class="WpfApplication125.MainWindow"
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:local="clr-namespace:WpfApplication125"
mc:Ignorable="d"
xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
Title="MainWindow" Height="350" Width="525">
<Grid>
<xcdg:DataGridControl x:Name="_dataGrid">
<xcdg:DataGridControl.Columns>
<xcdg:Column FieldName="OrderID"
Title="Order"
Width="100"
IsMainColumn="True">
</xcdg:Column>
<xcdg:Column FieldName="OrderDate"
Title="Order Date"
Width="150"/>
<xcdg:Column FieldName="ShipRegion"
Visible="False" />
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>
</Window>using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WpfApplication125
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_dataGrid.ItemsSource = new List<MyData>()
{
new MyData() { OrderID = 101, OrderDate = new DateTime( 2017, 5, 10), ShipRegion = "USA" },
new MyData() { OrderID = 122, OrderDate = new DateTime( 2017, 5, 15), ShipRegion = "Canada" },
new MyData() { OrderID = 133, OrderDate = new DateTime( 2017, 4, 25), ShipRegion = "Mexico" },
};
// This can also be done in MainWindow.Loaded event handler !
var dataTemplate = new DataTemplate();
var date = new FrameworkElementFactory( typeof( TextBlock ) );
date.SetBinding( TextBlock.TextProperty, new Binding( "." ) { Converter = new DisplayedValueConverter() } );
dataTemplate.VisualTree = date;
_dataGrid.Columns[ "OrderDate" ].CellContentTemplate = dataTemplate;
}
}
public class MyData
{
public int OrderID
{
get;
set;
}
public DateTime OrderDate
{
get;
set;
}
public string ShipRegion
{
get;
set;
}
}
public class DisplayedValueConverter : IValueConverter
{
#region IValueConverter Members
public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
{
if( value == null )
return null;
System.DateTime dateTime = ( System.DateTime )value;
return dateTime.ToString( "ddd MM/ dd / yy hh:mm tt" );
}
public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
{
throw new NotImplementedException();
}
#endregion
}
}――――
Get more controls, features, updates and technical support with Xceed Toolkit Plus for WPF