[英]ListBox.SelectedItem binding canceled when setting 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>
我的ViewModel:
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 = "chris@roedernet.de",
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。
非常感謝! 克里斯
我看到發布的代碼有幾個問題,可能是因為它不完整:
SelectedItem
:您正在將文本框等綁定到SelectedItem,但是您的VM上沒有此類屬性。 您可能打算綁定到MasterEmployee
。 (注意:我寧願將其命名為SelectedItem)。 我認為這是您所看到的根本原因。 SaveEntry
您可能會完全重新創建列表。 因此, MasterEmployee
將不再在該列表中。 我認為這可能會導致進一步的錯誤。 您正在顯示不在列表中的節點。 如果這樣做沒有幫助,請提供一個完整的簡單示例,因為您發布的代碼不完整並且無法編譯。 嘗試縮小問題范圍。
問題解決了,這是我的員工類的一個問題,其中我重寫了GetHashCode函數以返回employeeID。 這在編輯employeeID時使綁定停止工作...感謝您的努力!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.