2011-07-13 32 views
1

「不能從目標回源保存價值」,我有以下組合框:組合框的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.

可能是什麼原因?

+0

爲什麼你綁定到選定的值而不是選定的項目..? – Bathineni

+0

它已經是這樣了,改變它並不能解決問題 – Juan

+1

在我看來,SelectedUsuario setter會拋出一個NullReference異常。您是否嘗試過啓用異常:在Visual Studio中調試 - >異常並查看它崩潰的位置? – AlexDrenea

回答

1

這是內部ResetWorkspace(值)空例外,跟蹤並沒有幫助

謝謝大家!

+0

對我來說,它綁定到一個屬性類型錯誤 – erem

3

首先將ItemsSource的綁定更改爲OneWay。 TwoWay沒有意義。

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

你的ItemsSource UsuariosApp是UsuarioDetalle列表的類型,所以你SelectedUsuario屬性應的typeof UsuarioDetalle。變化的SelectedValue到的SelectedItem

public UsuarioDetalle SelectedUsuario { ... } 

你也必須添加OnPropertyChanged您SelectUsuario二傳手

OnPropertyChanged(() => SelectedUsuario); 
+1

綁定selectedItem TwoWay確實有道理。否則,你不能通過綁定 – oXeNoN

+3

更好的downvote更新視圖模型.... SelectedItem有雙向綁定通過DEFAULT,所以你不需要在xaml中設置它。如果你再讀一遍,你會看到我寫道,TwoWay對於ItemsSource綁定沒有意義! – blindmeis

相關問題