Search code examples
wpfxamlbuttonbindingdatagrid

WPF Datagrid Button Datatrigger


Working on a WPF Datagrid. The datagrid has a bound source with a column "DateInSource." The value in the column "DateInSource" can either be NULL or a date.

If the value is null I want the button RED and the words on the button should be the "Receive Work."

IF the value is not null I want the button GREEN and the words on the button should be the value of DateInSource.

I have created a style resource:

<Style TargetType="{x:Type Button}" x:Key="ButtonFormatStyle" >
    <Setter Property="Background" Value="Green"/>
    <Setter Property="Content" Value="{Binding}"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding DateInSource}" Value="X:null" >
            <Setter Property="Background" Value="Red" />
            <Setter Property="Content" Value="Receive Work" />
        </DataTrigger>
    </Style.Triggers>
</Style>

I have created a button:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button x:Name="BtnDateInSource" Click="ReceiveWork" Content="{Binding DateInSource}"  Style="{StaticResource ButtonFormatStyle}"></Button>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

It does not appear that my data trigger is working. Suggestions?

Current Result


Solution

  • Value="X:null" is not valid null-check. it is comparison with "X:null" string.

    change it like Value="{x:Null}"