Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF datagrid: disable editing in certain rows

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?

like image 733
jonny Avatar asked Dec 17 '09 11:12

jonny


1 Answers

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.

like image 184
Ben Collier Avatar answered Oct 07 '22 22:10

Ben Collier