开发者

Combobox SelectedValue "Cannot save value from target back to source"

I have the following combobox:

<Controls:RadComboBox 
  ItemsSource="{Binding UsuariosApp,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
  SelectedValue="{Binding SelectedUsuario}"
  IsEnabled="{Binding ChangeUserEnabled}"/>

Viewmodel:

public List<UsuarioDetalle> UsuariosApp
{
    get
    {
        if (_users == null)
        {
            _users = new List<UsuarioDetalle>();
            if (AuthenticationController.ChildUserEntities != null)
                _users.AddRange(AuthenticationController.ChildUserEntities);
        }
        return _users;
    }
    set
    {
        _users = value;
        OnPropertyChanged(() => UsuariosApp);
    }
}

public object SelectedUsuario
{
    get
    {
        if (UsuariosApp != null && UsuariosApp.Cou开发者_开发技巧nt > 0)
        {
            AuthenticationController.CurrentUser = UsuariosApp[0].idUsuario;
            AuthenticationController.CurrentUserRole =
                (RolesUsuario)UsuariosApp[0].idStTipoUsuario;

            _lastUser = UsuariosApp[0];

            return UsuariosApp[0];
        }
        return null;
    }
    set
    {
        if (!((UsuarioDetalle)_lastUser).idUsuario.ToString().Equals(((UsuarioDetalle)value).idUsuario.ToString()))
        {
            bool? confirmation = SwitchUserConfirmation();
            if (confirmation.HasValue && confirmation.Value.Equals(false))
            {
                // Alex: cancelar el cambio de valor del combo
                ChangeUser = _lastUser;
            }
            else
            {
                ResetWorkspace(value);
            }
        }
        else
        {
            ResetWorkspace(value);
        }
    }
}

It works but in the output I have the following error when I change the value of the combobox

System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=SelectedUsuario; DataItem='MainWindowViewModel' (HashCode=26603182); target element is 'RadComboBox' (Name='comboChildUsers'); target property is 'SelectedValue' (type 'Object') TargetInvocationException:'System.Reflection.TargetInvocationException: Se produjo una excepción en el destino de la invocación. ---> System.NullReferenceException: Referencia a objeto no establecida como instancia de un objeto.

What could be the reason?


first of all change your Binding for ItemsSource to OneWay. TwoWay makes no sense.

<Controls:RadComboBox 
     ItemsSource="{Binding UsuariosApp,Mode=OneWay}"
     SelectedItem="{Binding SelectedUsuario}"
     IsEnabled="{Binding ChangeUserEnabled}" />

your itemssource UsuariosApp is type of list of UsuarioDetalle, so your SelectedUsuario Property should be typeof UsuarioDetalle. Change SelectedValue to SelectedItem

public UsuarioDetalle SelectedUsuario { ... }

you will also have to add the OnPropertyChanged to your SelectUsuario setter

OnPropertyChanged(() => SelectedUsuario);


It was a null exception inside ResetWorkspace(value), the trace didn't help

Thanks everyone!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜