Question

I have something like orders table bound to a DataGrid. I want to disable editing on rows, where order date is expired (deadline date > current date): show them greyed and ban entering into edit mode.

Here is what I have:

<Style TargetType="{x:Type WPFToolkit:DataGridRow}">
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <!-- ??? order_date > current_date -->
                <!-- some other condition - already works -->
            </MultiTrigger.Conditions>
            <Setter Property="IsEnabled" Value="False"/>
            <Setter Property="Foreground" Value="LightGray"/>
        </MultiTrigger>
    </Style.Triggers>
</Style>

The order date is a template column defined as follows:

<WPFToolkit:DataGridTemplateColumn SortMemberPath="order_date" Header="Deadline" >
    <WPFToolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=order_date,  
                ConverterCulture='ru-RU', StringFormat='{}{0:d}'}" />
        </DataTemplate>
    </WPFToolkit:DataGridTemplateColumn.CellTemplate>
    <WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <WPFToolkit:DatePicker SelectedDate="{Binding Path=order_date}" />
        </DataTemplate>
    </WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
</WPFToolkit:DataGridTemplateColumn>

Is there a way to disable editing mode in rows where order date has expired?

Was it helpful?

Solution

So it seems that everything works but you need a way to check (order_date > current_date) in a Trigger? If that is the case, you could write a ValueConverter like perhaps DateExpiredConverter and check the condition there and return a boolean.

The converter class might look something like this:

public class DateExpiredConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime order_date = (DateTime)value;

        if (order_date > System.DateTime.Now)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}

Then in your trigger you do something like this:

<Style TargetType="{x:Type WPFToolkit:DataGridRow}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=order_date, Converter={StaticResource DateExpiredConverter}}" Value="True">
                <Setter Property="IsEnabled" Value="False"/>
                <Setter Property="Foreground" Value="LightGray"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

I haven't tested this code but it should be pretty close to what you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top