Well, this little quirk of WPF
is really getting under my skin. So, I have a DataGrid
like this:
<DataGrid x:Name="Rates" AlternatingRowBackground="#FCFCFC" AutoGenerateColumns="False" CanUserReorderColumns="True" CanUserResizeColumns="True" CanUserAddRows="False" SelectionUnit="FullRow" CanUserSortColumns="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Item" Width="Auto" IsReadOnly="True" Binding="{Binding item}"/>
<DataGridTextColumn Header="Rate" Width ="*" Binding="{Binding rate, UpdateSourceTrigger=PropertyChanged}"/>
</DataGrid.Columns>
It simply has a list of items in column A and we have to enter the rates in column B. Sounds simple enough.
Here is the property in the ViewModel
to which it is bound.
private BindableCollection<Rate> _rates;
public BindableCollection<Rate> Rates
{
get
{
return _rates;
}
set
{
if (value == _rates)
{
return;
}
_rates = value;
NotifyOfPropertyChange();
}
}
Here is the definition for Rate
:
public partial class Rate
{
public int ID { get; set; }
public string client { get; set; }
public string item { get; set; }
public Nullable<decimal> rate { get; set; }
}
So, the second column of my DataGrid
is getting bound to Rate.rate
which is a Nullable<decimal>
. Herein lies the problem. The Nullable<decimal>
does accept NULL
but the TextBox
wouldn't let me enter a blank value. Moreover, if I type in some value like 5 and then delete it, the ViewModel
retains the previous value.
I have seen solutions involving the IValueConverter
, but I would prefer a XAML-based solution. Is there any? I have several modules where the DataType
is Nullable
, and it can quickly become very tedious if I have to use IValueConverter
every time.
EDIT
I am using Caliburn.Micro
, hence using TargetNullValue=''
will not work everywhere, as the binding is automatic. It'll work in the above example, where the binding
is explicitly mentioned.