WPF datagrid: disable editing in certain rows

假如想象 提交于 2019-12-04 02:44:07
Ben Collier

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!