views:

8

answers:

0

ConvertDateTime - return good string "01.10.2010" but datapicker not binding.

if we replace datapicker on textbox - all works well

help me, code:

add:

 xmlns:loc="clr-namespace:StoreBags"

add:

 <loc:ConvertDateTime x:Key="conkey"/>

xaml add:

 <my:DataGridTemplateColumn Header="Дата" Width="100">
        <my:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <DockPanel>
                    <my:DatePicker Text="{Binding date, Converter={StaticResource conkey}}" x:Name="p_datePicker"/>
                </DockPanel>
            </DataTemplate>
        </my:DataGridTemplateColumn.CellTemplate>
    </my:DataGridTemplateColumn>

Converter:

public class ConvertDateTime : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                DateTime date = (DateTime)value;
                return date.ToShortDateString().ToString(); // return "01.10.2010"
            }
            catch { return null; }
        }

        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;
        }
    }