我的DataGrid
DataGridComboBoxColumn
列可以编辑。当我编辑它时抛出异常。调试后我发现Converter
收到了DependencyProperty.UnsetValue
,因此我必须通过返回""
来过滤掉它,这会导致显示空单元格。
我试图谷歌并理解为什么我得到DependencyProperty.UnsetValue
但没有运气。
有什么想法吗?
public UserControl()
{
InitializeComponent();
DataContext = new MyViewModel();
}
public class MyViewModel : ViewModelBase
{
public ObservableCollection<MyItem> MyItems { get; private set; }
public MyViewModel()
{
LoadStates();
}
public void LoadStates()
{
MyItems = new ObservableCollection<MyItem>(DataProvider.GetList());
//...
}
}
public class MyItem
{
public bool Start {get; set;}
}
public class BoolToStatusConverter : IMultiValueConverter
{
private MyViewModel _model;
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
_model = values[0] as MyViewModel ;
//TODO: Why I get UnsetValue??
if (values[1] == DependencyProperty.UnsetValue) return "";
//Using _model
//...
return (bool)values[1] ? Statuses.Start : Statuses.Stop;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return new object[] { _model, (Statuses)value == Statuses.Start };
}
}
<UserControl.Resources>
<local:BoolToStatusConverter x:Key="BoolToStatusConverter" />
<ObjectDataProvider x:Key="myEnum" MethodName="GetValues" ObjectType="{x:Type core:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type Type="local:Statuses"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.Resources>
<Grid>
<DataGrid ItemsSource="{Binding MyItems}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Status" ItemsSource="{Binding Source={StaticResource myEnum}}">
<DataGridComboBoxColumn.SelectedItemBinding>
<MultiBinding Converter="{StaticResource BoolToStatusConverter}">
<Binding Path="DataContext"
RelativeSource="{RelativeSource AncestorType={x:Type UserControl}}" />
<Binding Path="Start" />
</MultiBinding>
</DataGridComboBoxColumn.SelectedItemBinding>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
答案 0 :(得分:0)
为什么不使用常规IValueConverter?在这种情况下,我不确定你是否真的需要一个IMultiValueConverter。这对我有用:
public class BoolToStatusConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool) value)
{
return Statuses.Start;
}
return Statuses.Stop;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var theValue = (Statuses)value;
if (theValue == Statuses.Start)
{
return true;
}
return false;
}
}
和XAML:
<DataGrid ItemsSource="{Binding MyItems}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Status" ItemsSource="{Binding Source={StaticResource myEnum}}">
<DataGridComboBoxColumn.SelectedItemBinding>
<Binding Converter="{StaticResource BoolToStatusConverter}" Path="Start">
</Binding>
</DataGridComboBoxColumn.SelectedItemBinding>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
另外,为了进一步简化它,只需将MyItem中的字段设置为状态,并完全松开转换器。