What I have is a DataGrid (in WPF), and I have bound it to a list of a custom class I have made.
Lets say for simplicity sake, the class is follows:
public class MyClass{
int val;
string str;
static const int alwaysSetValue = 10;
}
Is there a way to (in the databinding or the class itself) say that “if val = -1, in the datagrid instead of displaying -1, just display a blank, or ‘ ‘?
I was looking at the Binding’s IsTargetNull value, and that would be good if int was a nullable type, but I would prefer to not use int? if possible.
Is there any way to do this? Some sort of override ToString() or something?
Solution
see answer below. The only change I made was to set the binding & convert in code:
DataGrid.Columns.Add(new DataGridTextColumn() { Header = "Value", Binding = new Binding("val") { Converter = new MyValConverter() } });
Here is example:
XAML file:
Code-behind file: