我正在使用RowStyleSelector根据项目为我的Datagridrow着色。绘制行时它工作正常,但在项上的属性更改时不会触发它。由于 inotifyPropertyChange,该项的值显示在数据网格中。
ListViewFileList.RowStyleSelector= new ErrorStyleSelector();
enter code here
private class ErrorStyleSelector : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
if (item is myFile)
{
if ((item as myFile).ErrorStatus.IsErrorfile())
{
Style st = new Style(typeof(DataGridRow));
st.Setters.Add(new Setter(BackgroundProperty, Brushes.Red));
return st;}
}
}
我想在项目更改时触发行样式选择器。不使用 xaml。
编辑
我最终使用了IvalueConverter
Style st = new Style(typeof(DataGridRow));
DataTrigger tig = new DataTrigger()
{
Binding = new Binding(nameof(myFile.ErrorStatus))
{
Converter = new ConverterError(),
}
};
st.Triggers.Add(tig);
ListViewFileList.RowStyle = st;
private class ConverterError : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
TRCFile obj = value as myError;
if (obj!= null && othercondition)
{
Style st = new Style(typeof(DataGridRow));
return st;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
>引发PropertyChanged
事件时,StyleSelector
不会创建新样式。
您应该将IsErrorfile()
设置为公共属性,并使用Style
中的DataTrigger
绑定到它:
myFile myFile = item as myFile;
if(myFile != null)
{
Style st = new Style(typeof(DataGridRow));
DataTrigger dataTrigger = new DataTrigger()
{
Binding = new Binding("ErrorStatus.IsErrorfile"),
Value = true
};
dataTrigger.Setters.Add(new Setter(BackgroundProperty, Brushes.Red));
st.Triggers.Add(dataTrigger);
return st;
}