Using WPF DataGrid, I need to change the various display and related properties of the DataGridCell - such as Foreground, FontStyle, IsEnabled, etc. - based on the corresponding property value of the cell object.
Now this is easy to do in code, for example (using the Observable Collection of ObservableDictionaries):
var b = new Binding("IsLocked") { Source = row[column], Converter = new BoolToFontStyleConverter() }; cell.SetBinding(Control.FontStyleProperty, b);
and it works fine, however, I cannot figure out how to do this in XAML, since I cannot find a way to set the Path property of the cell object.
One XAML attempt:
<Setter Property="FontStyle"> <Setter.Value> <MultiBinding Converter="{StaticResource IsLockedToFontStyleConverter}" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"> <Binding /> <Binding RelativeSource="{x:Static RelativeSource.Self}"/> </MultiBinding> </Setter.Value> </Setter>
but no binding to the IsLocked property
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var row = (RowViewModel) values[0]; var cell = (DataGridCell) values[1]; if (cell != null && row != null) { var column = DataGridMethods.GetColumn(cell); return row[column].IsLocked ? "Italic" : "Normal"; } return DependencyProperty.UnsetValue; }
Note that the previous version returned the string [col] .IsLocked and set FontStyle using a DataTrigger, but the returned object is not bound to the database.
Please note, of course, that the application does not know which columns are at design time.
Finally, DataTable is too inefficient for my requirements, but I would be interested to see how it is done with DataTables anyway, if there is such a solution for them, it can be useful elsewhere (although I prefer to use collections).
Of course, this is a common problem, and I'm a WPF newbie trying to transfer all MVVMs to my project, but this problem keeps me in regards to using DataGrid WPF.