Reputation: 73
<DataGrid....
<DataGrid.Resources>
<DataTemplate DataType="{x:Type DateTime}">
<TextBlock Text="{Binding StringFormat={}{0:d}}" />
</DataTemplate>
</DataGrid.Resources>
...
<DataGridTemplateColumn Header="Время оплаты">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center" Text="{Binding date_payment}" Width="Auto" Height="Auto" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
but this column have type DateTime
...
i need convert this column on this type(string), because i need rename value row on this column, i use event LoadingRow
so
DataRowView item = e.Row.Item as DataRowView;
DataRow row = item.Row;
var time = row[4];
if (Convert.ToString(time) == "01.01.0001 0:00:00")
{
row[4] = "No payment";
}
but its wrong, row no converting to string, please help
Upvotes: 1
Views: 4508
Reputation: 20086
First of all, you have both a cell template and a data template. Pick one. Second, since you have a data template anyway there's no reason to create a converter, much less a code-behind event handler. You can keep all the relevant code and text strings (what if you need to localize?) nicely in one place with a trigger:
<DataTemplate TargetType="{x:Type DateTime}">
<TextBlock x:Name="text" Text="{Binding StringFormat={}{0:d}}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding}" Value="{x:Static DateTime.MinValue}">
<Setter TargetName="text" Property="Text" Value="No payment"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Upvotes: 3
Reputation: 3298
You shoud use Converter for this:
public class MyConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
if (value == DateTime.MinValue) {
return "No payment";
}
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
}
Than just add the converter to your binding.
Upvotes: 1
Reputation: 7847
In case if it's Nullable value you could use
Binding="{Binding date_payment, TargetNullValue={}Дата отсутствует}"
In case if not, use IValueConverter where you will check for MinDate. Here is an Example how to use converters, and converter for you
public class DateConverter:IValueConverter
{
private const string NoDate = "Дата отсутствует";
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is DateTime)
{
var date = (DateTime) value;
if(date==DateTime.MinValue)
return NoDate;
return date.ToString();
}
return NoDate;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Upvotes: 2