2017-08-01 176 views
0

时,我有一些问题,数据绑定...这里的情况是:ListBox.SelectedItem取消绑定设置的SelectedIndex

我的观点:

<Window x:Class="Shifter.Forms.Employee.frmEditEmployee" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     Title="frmEditEmployee" Height="350.141" Width="497.195" WindowStyle="None" ResizeMode="NoResize" Foreground="Blue" WindowStartupLocation="CenterScreen">   

    <Grid>  
     <ListBox x:Name="lstEmployee" IsEnabled="{Binding NoEditMode}" SelectedItem="{Binding MasterEmployee}" ItemsSource="{Binding Path=ListOfEmployees}" HorizontalAlignment="Left" Height="276" Margin="25,19,0,0" VerticalAlignment="Top" Width="217" /> 
     <TextBox x:Name="txtForename" Text="{Binding SelectedItem.Forname, ElementName=lstEmployee}" Margin="342,21,0,0" GotFocus="SelectText"/> 
     <TextBox x:Name="txtLastname" Text="{Binding SelectedItem.Lastname, ElementName=lstEmployee}" Margin="342,47,0,0" GotFocus="SelectText"/> 
     <TextBox x:Name="txtShowingname" Text="{Binding SelectedItem.Showingname, ElementName=lstEmployee}" Margin="342,74,0,0" GotFocus="SelectText"/> 
     <TextBox x:Name="txtPersonelNumber" Text="{Binding SelectedItem.EmployeeID, ElementName=lstEmployee}" Margin="342,99,0,0" GotFocus="SelectText"/> 
     <DatePicker x:Name="dtpBirthday" SelectedDate="{Binding SelectedItem.Birthday, ElementName=lstEmployee}" HorizontalAlignment="Left" Margin="342,125,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.256,0.417" SelectedDateFormat="Short"/> 
     <TextBox x:Name="txtLoan" Text="{Binding SelectedItem.Loan, ElementName=lstEmployee}" Margin="342,151,0,0" TextWrapping="Wrap" GotFocus="SelectText"/> 
     <TextBox x:Name="txtPhone" Text="{Binding SelectedItem.Telephone, ElementName=lstEmployee}" Margin="342,229,0,0" TextWrapping="Wrap" GotFocus="SelectText"/> 
     <TextBox x:Name="txtEMail" Text="{Binding SelectedItem.EMail, ElementName=lstEmployee}" Margin="342,255,0,0" TextWrapping="Wrap" GotFocus="SelectText"/> 
     <ComboBox x:Name="cmbContract" ItemsSource="{Binding ListOfContracts, Mode=TwoWay}" SelectedItem="{Binding SelectedItem.Contract, ElementName=lstEmployee, Mode=TwoWay}" HorizontalAlignment="Left" Margin="342,179,0,0" VerticalAlignment="Top" Width="130"/> 
     <ComboBox x:Name="cmbGroup" ItemsSource="{Binding ListOfGroups, Mode=TwoWay}" SelectedItem="{Binding SelectedItem.Group, ElementName=lstEmployee, Mode=TwoWay}" HorizontalAlignment="Left" Margin="342,204,0,0" VerticalAlignment="Top" Width="130"/> 
     <CheckBox x:Name="chkHide" Content="MA im Dienstplan ausblenden" IsChecked="{Binding SelectedItem.isHiding, ElementName=lstEmployee}" HorizontalAlignment="Left" Margin="259,280,0,0" VerticalAlignment="Top" ToolTip="Der Mitarbeiter wird nicht im Dienstplan angezeigt (beispielsweise wegen längerer Abwesenheit)" Width="211"/>    

     <Button x:Name="btnAdd" Content="Add" Command="{Binding Path=cmdAdd, Mode=TwoWay}" HorizontalAlignment="Left" Height="35" Margin="25,303,0,0" VerticalAlignment="Top" Width="35"> 
     </Button> 
     <Button x:Name="btnEdit" Content="Edit" Command="{Binding Path=cmdEdit}" HorizontalAlignment="Left" Height="35" Margin="70,303,0,0" VerticalAlignment="Top" Width="35"> 
     </Button> 
     <Button x:Name="btnDelete" Content="Delete" Command="{Binding Path=cmdDelete}" HorizontalAlignment="Left" Height="35" Margin="115,303,0,0" VerticalAlignment="Top" Width="35"> 
     </Button> 
     <Button x:Name="btnCancel" Content="Cancel" Command="{Binding Path=cmdCancel}" HorizontalAlignment="Left" Height="35" Margin="391,303,0,0" VerticalAlignment="Top" Width="35"> 
     </Button> 
     <Button x:Name="btnOK" Content="OK" Command="{Binding Path=cmdOK}" HorizontalAlignment="Left" Height="35" Margin="436,303,0,0" VerticalAlignment="Top" Width="35"> 
     </Button> 
    </Grid> 
</Window> 

我的视图模型:

namespace Models 
{ 
    public class VM_EditEmployee : INotifyPropertyChanged 
    { 
     #region Propertys 
     private ObservableCollection<Common.Employee> mListOfEmployees; 
     private ObservableCollection<Common.EmployeeContract> mListOfContracts; 
     private ObservableCollection<Common.EmployeeGroup> mListOfGroups; 
     private Common.Employee mMasterEmployee; 
     private bool isNew; 
     private Employee.frmEditEmployee EmpoyeeView; 

     public event PropertyChangedEventHandler PropertyChanged; 

     public ObservableCollection<Common.Employee> ListOfEmployees 
     { 
      get 
      { 
       return mListOfEmployees; 
      } 

      set 
      { 
       mListOfEmployees = value; 
       OnPropertyChanged("ListOfEmployees"); 
      } 
     } 
     public ObservableCollection<EmployeeContract> ListOfContracts 
     { 
      get 
      { 
       return mListOfContracts; 
      } 

      set 
      { 
       mListOfContracts = value; 
       OnPropertyChanged("ListOfContracts"); 
      } 
     } 
     public ObservableCollection<EmployeeGroup> ListOfGroups 
     { 
      get 
      { 
       return mListOfGroups; 
      } 

      set 
      { 
       mListOfGroups = value; 
       OnPropertyChanged("ListOfGroups"); 
      } 
     } 
     public Common.Employee MasterEmployee 
     { 
      get 
      { 
       return mMasterEmployee; 
      } 

      set 
      { 
       mMasterEmployee = value; 
       OnPropertyChanged("MasterEmployee"); 
      } 
     } 


     public ICommand cmdAdd { get; set; } 
     public ICommand cmdEdit { get; set; } 
     public ICommand cmdDelete { get; set; } 
     public ICommand cmdCancel { get; set; } 
     public ICommand cmdOK { get; set; } 

     #endregion 

     public VM_EditEmployee(Employee.frmEditEmployee tmpView) 
     { 
      EmpoyeeView = tmpView; 
      cmdAdd = new RelayCommand(o => AddEntry()); 
      cmdEdit = new RelayCommand(o => EditEntry()); 
      cmdDelete = new RelayCommand(o => DeleteEntry()); 
      cmdCancel = new RelayCommand(o => Cancel()); 
      cmdOK = new RelayCommand(o => SaveEntry()); 

      ListOfEmployees = Database_Employee.GetListOfEmployee(); 
      ListOfContracts = Database_Contract.GetListOfContract(); 
      ListOfGroups = Database_Group.GetListOfGroups(); 
     } 

     protected internal void OnPropertyChanged(string propertyname) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); 
     } 

     private void DeleteEntry() 
     { 
      if (MessageBox.Show("Sure you want to delete?", "Question", MessageBoxButton.OKCancel) == MessageBoxResult.OK) 
      { 
       Database_Employee.DeleteEmployee(MasterEmployee); 
       ListOfEmployees = Database_Employee.GetListOfEmployee(); 
      } 
     } 

     private void Cancel() 
     { 

     } 

     private void AddEntry() 
     { 
      isNew = true; 
      Common.Employee newEmployee = new Common.Employee() 
      { 
       Forname = "Max", 
       Lastname = "Mustermann", 
       Showingname = "Max", 
       EmployeeID = 666, 
       Birthday = new System.DateTime(1980, 5, 5), 
       Loan = "9,50", 
       Contract = Database_Contract.GetListOfContract()[0], 
       Group = Database_Group.GetListOfGroups()[0], 
       Telephone = "012456789", 
       EMail = "[email protected]", 
       isHiding = false 
      }; 

      ListOfEmployees.Add(newEmployee); 
      MasterEmployee = newEmployee; 
     } 

     private void EditEntry() 
     { 
      isNew = false; 
     } 

     private void SaveEntry() 
     { 
       if (isNew == true) 
       { 
        Database_Employee.CreateEmployee(MasterEmployee);      
       } 
       else     { 
        Database_Employee.EditEmployee(MasterEmployee); 
       } 
       ListOfEmployees = Database_Employee.GetListOfEmployee(); 
      } 
      else // Wenn der EditMode nict aktiv ist 
      { 
       EmpoyeeView.Close(); 
      } 


     } 
    } 
} 

的“MasterEmployee”属性用于访问ViewModel中的选定项目以保存员工中的更改。 一切工作正常,列表框填充数据,并在列表框选择员工的详细信息在文本框中正确显示(有更多的则只是这一个,但它没有必要这个问题)。

当我创建一个新员工时,我创建了一个新员工类的实例,用一些占位符信息填充它,并将MasterEmployee的引用设置为这个新员工,因为我想编辑文本框中的新员工的看法。然后,我编辑新员工,保存更改并想要转到ListView中的其他员工,但没有任何反应。我猜是因为当我设置MasterEmployee的引用时,与ListBox的绑定丢失了。

所以我的问题是:我怎样才能解决这个问题?我想保留MVVM模式,意思是通过代码设置绑定,我需要访问viewmodel中的视图,那些不是MVVM。

非常感谢! 克里斯

+1

_“我猜这是因为当我设置MasterEmployee的参考,与列表框丢失绑定” _ - 没了。如果在代码后面直接设置'SelectedItem',绑定将会丢失。事实上,设置'MasterEmployee'属性_is_是更改所选项目的正确方法。你的代码还有其他错误。但如果没有一个可靠地再现问题的好的[mcve],就不可能有人能够确切地说出什么是错的。 –

+0

在Model_EditEmployee中,ListOfEmployees属性设置为新集合看起来很奇怪。这显然会使MasterEmployee属性无效,因为它的值不再包含在ListOfEmployees中。 – Clemens

+0

我认为问题出在这个陈述中:_“并且想要去另一个员工”_。保存后如何选择其他项目? –

回答

1

我看到几个问题与发布的代码,可能是因为它是不完整:

  • SelectedItem:你的文本框等绑定到的SelectedItem但在VM上没有这样的属性。您可能的意思是绑定到​​。 (注意:我宁愿将该名称命名为SelectedItem)。我认为这是你所看到的根源。
  • 当你执行SaveEntry你可能完全重新创建列表。因此​​将不再在该列表中。我认为这可能会导致更多的错误。您正在显示不在列表中的节点。

如果这不起作用,请提供一个完整的简单示例,因为您发布的代码不完整,无法编译。尝试缩小你的问题。

-1

问题解决了,这是我的员工类的问题,我不得不重写的GetHashCode的函数返回的雇员。当编辑员工ID时,这会使绑定制动器...感谢您的努力!