0

I created a button column as a template column in a WPF datagrid. When you press the button it sets the button content to the current date.

<DataGridTemplateColumn Header="Date In Source">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button  x:Name="BtnDateInSource" Click="DateButton"  Style="{StaticResource ReceiveWorkButton}" ></Button>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>



<Style TargetType="{x:Type Button}" x:Key="ReceiveWorkButton" >
    <Setter Property="Background" Value="Green"/>
    <Setter Property="Content" Value="{Binding DateInSource}"/>
    <Setter Property="IsEnabled" Value="False"/>            
    <Style.Triggers>
        <DataTrigger Binding="{Binding DateInSource}" Value="{x:Null}" >
            <Setter Property="Background" Value="#A3BF3B" />
            <Setter Property="Content" Value="Receive Work" />
            <Setter Property="IsEnabled" Value="True"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

The current problem is that since it is a template column I can't seem to bind it to the datasource (MVVM).

So the user presses the button, and it changes the button text to the current date/time. I now want to write that back to the database.
My MVVM observable collection is InspectionStatus in class SourceInspection.

I would like to do it in XAML but maybe it has to be done with INotifyPropertyChanged?

DatagridContext: Code Behind:

    System.Windows.Data.CollectionViewSource SourceViewSource =
    ((System.Windows.Data.CollectionViewSource) 
    (this.FindResource("SourceViewSource")));
    _context.SourceInspections.Load();
    SourceViewSource.Source = _context.SourceInspections.Local;

I think I figured it out, used this... updating wpf datagrid on button click

Regards K.

Kerry
  • 57
  • 8
  • How did you set a grid datacontext? Is it an `InspectionStatus` collection? You can use `Binding` for button content – Pavel Anikhouski Nov 22 '19 at 19:32
  • 1
    Does this answer your question? [DataTrigger Binding in WPF Style](https://stackoverflow.com/questions/17598200/datatrigger-binding-in-wpf-style) – Pavel Anikhouski Nov 22 '19 at 19:36
  • The original question has been modified to show code-behind for the Datagrid context – Kerry Nov 22 '19 at 20:36
  • I think I figured it out, I used this... [Button click update Solution](https://stackoverflow.com/questions/19772320/updating-wpf-datagrid-on-button-click) – Kerry Nov 22 '19 at 21:23
  • Possible duplicate of [updating wpf datagrid on button click](https://stackoverflow.com/questions/19772320/updating-wpf-datagrid-on-button-click) – Tiago Martins Peres Nov 23 '19 at 14:46

0 Answers0