「不能從目標回源保存價值」,我有以下組合框:組合框的SelectedValue
<Controls:RadComboBox
ItemsSource="{Binding UsuariosApp,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding SelectedUsuario}"
IsEnabled="{Binding ChangeUserEnabled}"/>
視圖模型:
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.Count > 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);
}
}
}
它的工作原理,但在輸出我有以下錯誤,當我更改組合框的值
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.
可能是什麼原因?
爲什麼你綁定到選定的值而不是選定的項目..? – Bathineni
它已經是這樣了,改變它並不能解決問題 – Juan
在我看來,SelectedUsuario setter會拋出一個NullReference異常。您是否嘗試過啓用異常:在Visual Studio中調試 - >異常並查看它崩潰的位置? – AlexDrenea