0

I have a WPF datagrid (I'm actually using Xceed DataGrid but I think the binding still works the same). I have a DatePicker column in my grid, but when I select a date, it doesn't update my underlying datasource. Any ideas?

I've already tried:

{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}
{Binding Path=., Mode=TwoWay}
{Binding Path=., UpdateSourceTrigger=PropertyChanged}
{Binding Path=.}
{Binding .}
{Binding}

XAML:

<xcdg:Column FieldName="NeedDateOverride" Title="NEED DATE (OVERRIDE)" Width="100">
    <xcdg:Column.CellContentTemplate>
        <DataTemplate x:Name="needDateOverrideTemplate">
            <DockPanel HorizontalAlignment="Stretch" LastChildFill="True">
                <DatePicker SelectedDate="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedDateChanged="NeedDateOverride_SelectedDateChanged" PreviewMouseDown="NeedDateOverride_GotFocus" CalendarOpened="NeedDateOverride_CalendarOpened"></DatePicker>
            </DockPanel>
        </DataTemplate>
    </xcdg:Column.CellContentTemplate>
</xcdg:Column>

C#:

public DateTime NeedDateOverride
{
    get
    {
        return this.needDateOverrideValue;
    }

    set
    {
        if (value != this.needDateOverrideValue)
        {
            this.needDateOverrideValue = value;
            NotifyPropertyChanged();
        }
    }
}
  • Try specifying the relative source. When I run into issues like this I try the relative source and it works. {Binding Path=.,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourAncestor}}} Sorry if the syntax is wrong I am typing it. Also not sure about your PropertyChanged. Usally I use PropertyChanged implementation that takes in the name like NotifyPropertyChanged("needDateOverrideValue"); – nikhil Jan 12 '19 at 01:38
  • The `DataContext` of your cell is the corresponding object that row. So `{Binding NeedDateOverride, ...}` should work, correct me if I am wrong and add more information: What's the source of your `DataGrid` / Where is `NeedDateOverride` defined / and so on – nosale Jan 12 '19 at 10:27

1 Answers1

0

Put the DatePicker in the EditTemplate of a CellEditor. Please refer to the documentation for an example. The CellContentTemplate is used to change the appearance of the cell when it's not in edit mode.

If you use the built-in DataGrid, you should use the CellEditingTemplate rather than the CellTemplate.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • This seemed pretty promising, but it still doesn't update my datasource when the selected date is changed. As a matter of fact, as soon as I click off of the DatePicker and it loses focus, it reverts back to the date that was previously in my datasource. – Christian Boyer Jan 14 '19 at 22:34
  • @ChristianBoyer: What if you bind the SelectedDate property to `{xcdg:CellEditorBinding}`? – mm8 Jan 15 '19 at 09:18
  • Ok, now the DatePicker keeps it's value when it loses focus, but my datasource still doesn't update – Christian Boyer Jan 16 '19 at 16:31