为什么我在使用多值转换器编辑后得到未设置值

本文关键字:设置 编辑 转换器 为什么 | 更新日期: 2023-09-27 17:56:47

DataGridDataGridComboBoxColumn列可以编辑的列。当我编辑它时,会抛出异常。调试后,我发现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>

为什么我在使用多值转换器编辑后得到未设置值

为什么不直接使用常规的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 a 状态中设置字段开始,并完全松开转换器。