DataGrid中的DatePicker不更新绑定对象

时间:2011-02-21 21:44:21

标签: wpf datagrid datepicker

我在DataGrid中有DatePicker:

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

它绑定到定义属性NextDate的对象(NextDate很长,我使用转换器在long和DateTime之间):

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

问题在于,当我在网格中更改日期时,通过键入或在下拉日历中选择,NextDate属性不会更改。 有什么想法吗?

1 个答案:

答案 0 :(得分:10)

尝试在Bindings中设置UpdateSourceTrigger。以下对我来说工作正常

    {li> PropertyChanged SelectedDate {li> LostFocus Text

此外,尝试在调试器中的转换器中设置断点以确保它被命中

<强>的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>