问题
I have 2 Datagrid checkbox columns each with a binding.
<DataGrid ItemsSource="{Binding}" Name="DataGrid1" DataContext="{Binding Source={StaticResource TableViewSource}}">
<DataGridCheckBoxColumn Header="Required" Width="50" MinWidth="50">
<DataGridCheckBoxColumn.Binding>
<Binding Path="Required" Converter="{StaticResource DateTimeToBooleanConverter}"/>
</DataGridCheckBoxColumn.Binding>
</DataGridCheckBoxColumn>
<DataGridCheckBoxColumn Header="Required Test" Width="60" MinWidth="60">
<DataGridCheckBoxColumn.Binding>
<MultiBinding Converter="{StaticResource DateTimeToBooleanMultiverter}">
<Binding Path="Required_Date" />
<Binding Path="Required_Time" />
</MultiBinding>
</DataGridCheckBoxColumn.Binding>
</DataGridCheckBoxColumn>
</DataGrid>
The bindings simply write the date/time the box was checked to the db field "Required"
and null when unchecked.
In the first checkbox everything works great with the following converter:
public class DateTimeToBooleanConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null) {
return true;
} else {
return false;
}
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == true) {
return System.DateTime.Now.Date;
} else {
return null;
}
}
}
In the last column I wanted to save the date and time into 2 different fields "Required_Date"
and "Required_Time" (String)
. The following IMultiConverter
was created to handle it:
public class DateTimeToBooleanMultiverter : IMultiValueConverter
{
public object Convert(object[] values, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values[0] != null) {
return true;
} else {
return false;
}
}
public object[] ConvertBack(object value, System.Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null) {
if (value == true) {
return {
System.DateTime.Now.Date,
System.DateTime.Now.TimeOfDay.ToString()
};
}
}
return {
null,
null
};
}
}
On initial load the column looks good. The column has all the appropriate cell checked. However, when the value is checked/unchecked I get the following binding error.
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='True' MultiBindingExpression:target element is 'DataGridCell' (Name=''); target property is 'CellContent' (type 'String')
Further, when I uncheck a cell it doesn't uncheck properly and I notice through some breakpoints in my converter that values are coming in as DependencyProperty.Unset
(Probably due to the binding warning).
(9-30-12)
I found a more promising lead in a codeplex comment:
"The reason that MultiBindings are not working is that DataGridHelper.UpdateSource uses GetBindingExpression and not GetBindingExpressionBase. If this is changed multi and priority bindings should work."
I attempted to set updating to Explicit
and create a DataGrid_EndCellEdit
event that calls the BindingBaseExpression
to update but I think the DataGridColumn is ignoring the update option.
(10-12-12)
Still no luck, I tried working with BindingBaseExpression
to no avail. Obviously the IMultiValueCOnverter
binding is doing something odd.
来源:https://stackoverflow.com/questions/12073597/binding-to-datagridcolumn-with-imultivalueconverter-encounters-issues-while-usin