Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatePicker in DataGrid does not update bound object

I have DatePicker in DataGrid:

<DataGridTemplateColumn Header="Next Date" Width="100" >
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <DatePicker SelectedDate="{Binding NextDate, Mode=TwoWay, Converter={StaticResource dateConverter}}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <DatePicker Text="{Binding NextDate, Mode=TwoWay, Converter={StaticResource dateConverter}}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

It is bound to object which defines property NextDate (NextDate is long an I use converter to go between long and DateTime):

public long NextDate
{
    get { return _nextDate; }
    set
    {
        if (_nextDate != value)
        {
            _nextDate = value;
            NotifyPropertyChanged("NextDate");
        }
    }
}

The problem is that when I change date in grid, either by typing or by selecting in drop-down calendar, NextDate property does not change. Any ideas?

like image 814
Vadim Avatar asked Feb 21 '11 21:02

Vadim


1 Answers

Try to set the UpdateSourceTrigger in the Bindings. The following was working fine for me

  • PropertyChanged for SelectedDate
  • LostFocus for Text

Also, try to set a breakpoint in the converter in the debugger to be sure it gets hit

Xaml

<DataGridTemplateColumn Header="Next Date" Width="100" >
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <DatePicker SelectedDate="{Binding NextDate,
                                       Mode=TwoWay,
                                       UpdateSourceTrigger=PropertyChanged,
                                       Converter={StaticResource dateConverter}}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <DatePicker Text="{Binding NextDate,
                                       Mode=TwoWay,
                                       UpdateSourceTrigger=LostFocus,
                                       Converter={StaticResource dateConverter}}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
like image 111
Fredrik Hedblad Avatar answered Nov 03 '22 07:11

Fredrik Hedblad