Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WPF DataGrid Converters

Tags:

c#

wpf

datagrid

I've been trying to format fields in a datagrid for days now. How can I simply change the Period is a date field from access. In this attempt I keep getting the error:

'{local:DateConverter}' value is not a valid MarkupExtension expression. Cannot resolve 'DateConverter' in namespace 'clr-namespace:Yabba'. 'DateConverter' must be a subclass of MarkupExtension.

However the examples I was working from all show DateConverter : IValueConverter.

I'm pretty much just want to change the column to list whatever I want based on the date. But can't get any 1 example/method to work.

XAML

<Window Name="MainForm" x:Class="Yabba.MainWindow"
    xmlns:local="clr-namespace:Yabba"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="655.217" Width="887.851" Loaded="Window_Loaded">
<Window.Resources>
    <local:DateConverter x:Key="dateConverter"/>
</Window.Resources>
<Grid>
    <DataGrid Name="dataGrid1"  AutoGenerateColumns="False" PreviewKeyDown="dataGrid1_KeyDown" CanUserAddRows="false" SelectionUnit="FullRow" IsReadOnly="True" SelectionMode="Single" HorizontalAlignment="Left" VerticalAlignment="Top" Height="348" Width="753" SelectionChanged="dataGrid1_SelectionChanged" Margin="0,20,0,0" MouseDoubleClick="dataGrid1_MouseDoubleClick">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Question" Binding="{Binding title}"></DataGridTextColumn>
            <DataGridTextColumn Header="Period" Binding="{Binding started, Converter={local:DateConverter}}"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

Code

namespace Yabba {
/// <summary>
[ValueConversion(typeof(DateTime), typeof(String))]
public class DateConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        DateTime date = (DateTime)value;
        return date.ToShortDateString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        string strValue = value as string;
        DateTime resultDateTime;
        if (DateTime.TryParse(strValue, out resultDateTime)) {
            return resultDateTime;
        }
        return DependencyProperty.UnsetValue;
    }
}

What am I doing wrong here?

Added notes to anyone using this as an example: (Unrelated to question, view selected answer for answer)

You may need to change the types depending.

[ValueConversion(typeof(DateTime), typeof(String))]

I had to change mine to

[ValueConversion(typeof(String), typeof(String))]

Then recast to DateTime

DateTime date = DateTime.Parse((string)value);
like image 289
Matty Avatar asked Mar 29 '13 03:03

Matty


Video Answer


1 Answers

Converter={local:DateConverter}}

Is wrong. Use this instead:

Converter={StaticResource dateConverter}}

Pay attention to the lowercase "d". Resource names are case-sensitive.

like image 168
Federico Berasategui Avatar answered Sep 22 '22 14:09

Federico Berasategui