WPF组合框,绑定异常

时间:2011-03-01 10:57:26

标签: wpf data-binding binding

  

可能重复:
  PowerShell WPF DataGrid: Exception thrown commiting emtpy row

这个问题让我疯狂..以下数据网格:

<StackPanel  DataContext="{DynamicResource currentFlow}"
             Visibility="{Binding Mode=OneWay, Converter={StaticResource CurrentFlowToVisibilityConverter}, ConverterParameter=Slutgodkendelse}">
    <StackPanel Orientation="Horizontal">
        <Label>Slutgodkender</Label>
        <csla:PropertyStatus Property="{Binding Slutgodkendelse.FlowStepBruger}"/>
    </StackPanel>
    <ComboBox x:Name="SlutgodkenderComboBox">
    <ComboBox.ItemsSource>
        <MultiBinding Converter="{StaticResource IsBrugerMemberOfRegnskabSorter}" ConverterParameter="Slutkontrollanter">
              <Binding Path="Slutkontrollant" Source="{StaticResource UIDataFactory}" />
              <Binding Path="SelectedItem" ElementName="RegnskabCombobox" />
        </MultiBinding>
    </ComboBox.ItemsSource>
    <ComboBox.SelectedItem>
        <Binding Path="Slutgodkendelse.FlowStepBruger" Converter="{StaticResource NullBusinessObjectToNullConverter}" ConverterParameter="ReadOnlyBruger" />
    </ComboBox.SelectedItem>
 </ComboBox>
</StackPanel>

给我这个例外:

System.UnhandledExceptionEventArgs
System.NullReferenceException: Object reference not set to an instance of an object.
   at MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid()
   at MS.Internal.Data.PropertyPathWorker.get_IsDBNullValidForUpdate()
   at MS.Internal.Data.ClrBindingWorker.get_IsDBNullValidForUpdate()
   at System.Windows.Data.BindingExpression.ConvertProposedValue(Object value)
   at System.Windows.Data.BindingExpressionBase.UpdateValue()
   at System.Windows.Data.BindingExpression.Update(Boolean synchronous)
   at System.Windows.Data.BindingExpressionBase.Dirty()
   at System.Windows.Data.BindingExpression.SetValue(DependencyObject d, DependencyProperty dp, Object value)
   at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at System.Windows.Controls.Primitives.Selector.UpdatePublicSelectionProperties()
   at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
   at System.Windows.Controls.Primitives.Selector.SelectionChanger.SelectJustThisItem(Object item, Boolean assumeInItemsCollection)
   at System.Windows.Controls.ComboBox.SelectItemHelper(Int32 startIndex, Int32 increment, Int32 stopIndex)
   at System.Windows.Controls.ComboBox.SelectPrev()
   at System.Windows.Controls.ComboBox.KeyDownHandler(KeyEventArgs e)
   at System.Windows.Controls.ComboBox.OnKeyDown(KeyEventArgs e)

转换器代码:

 public class IsBrugerMemberOfRegnskabSorter : IMultiValueConverter
{

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        var regnskab = values[1] as RegnskabNameAndId;
        var gruppeNavn = parameter as string;
        var brugere = (values[0] as ObservableCollection<ReadOnlyBruger>);

        ReadOnlyBruger currentBruger = null;

        try
        {
            currentBruger = values[2] as ReadOnlyBruger;
        }
        catch (IndexOutOfRangeException) { }

        if (brugere == null || regnskab == null ? true : regnskab.IsNull || String.IsNullOrEmpty(gruppeNavn))
        {
            return null;
        }

        var brugereFiltered = brugere.Where(b => !b.IsNull).Where(b => b.MemberOfRegnskaber.Contains(
            new RegnskabAndGruppeName { RegnskabId = regnskab.Id, GruppeName = gruppeNavn }))
            .Distinct(new HF.LambdaComparer<ReadOnlyBruger>((b1, b2) => b1.Id == b2.Id)).ToList();

        if (currentBruger != null)
            if (!brugereFiltered.Any(b => b.Id == currentBruger.Id))
                brugereFiltered.Insert(0, currentBruger);


        brugereFiltered.Insert(0, ReadOnlyBruger.GetNullBruger());

        return brugereFiltered;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}



  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        var typeOfBO = parameter as string;

        if (value == null && typeOfBO == "ReadOnlyBruger")
            return ReadOnlyBruger.GetNullBruger();

        if (value == null && typeOfBO == "ReadOnlyGroup")
            return ReadOnlyGruppe.GetNullGroup();

        if (value == null && typeOfBO == "RegnskabNameAndId")
            return RegnskabNameAndId.GetNullRegnskab();

        if (value == null && typeOfBO == "KontoFeltUI")
            return Bogføringslinje.KontoFeltUI.GetNullKontoFeltUI();

        if (value == null && typeOfBO == "DimensionFeltUI")
            return Bogføringslinje.DimensionFeltUI.GetNullDimensionFeltUI();


        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        if (value == null)
            return value;



        if (value is ReadOnlyBruger)
        {
            var bo = value as ReadOnlyBruger;
            if (bo.IsNull)
                return null;
            else
                return value;

        }
        else if (value is ReadOnlyGruppe)
        {
            var bo = value as ReadOnlyGruppe;
            if (bo.IsNull)
                return null;
            else
                return value;
        }
        else if (value is RegnskabNameAndId)
        {
            var bo = value as RegnskabNameAndId;
            if (bo.IsNull)
                return null;
            else
                return value;

        }
        else if (value is Bogføringslinje.KontoFeltUI)
        {
            var bo = value as Bogføringslinje.KontoFeltUI;
            if (bo.IsNull)
                return null;
            else
                return value;
        }
        else if (value is Bogføringslinje.DimensionFeltUI)
        {
            var bo = value as Bogføringslinje.DimensionFeltUI;
            if (bo.IsNull)
                return null;
            else
                return value;
        }


        return value;

    }

如果我按看似随机间隔多次按下keydown或keyup,就会发生这种情况。 转换器代码不会在其他任何地方引起问题。

有什么建议吗?

0 个答案:

没有答案