I have a little problem with multiselect and SelectedItem property. My application works in following way: When I click on a record in Listview, data from this record displays in textboxes. And now here is my problem. I want to achieve funcionality like: When I click on a record one after another I want to display data of last selected item. Unfortunately SelectedItem works only for first element. Could you help me? I attached necessary portion of code:
MainWindow.xaml
<ListView Name="EmployeeListView" HorizontalAlignment="Left" Height="160" Margin="0,153,0,0" VerticalAlignment="Top" Width="755" Grid.Row="1" ItemsSource="{Binding FilteredCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedEmployee, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedIndex="{Binding SelectedIndex}" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridViewColumn Header="EmployeeName" Width="150" DisplayMemberBinding="{Binding EmployeeName}" />
<GridViewColumn Header="EmployeeID" Width="150" DisplayMemberBinding="{Binding EmployeeID}" />
<GridViewColumn Header="EmployeeSalary" Width="150" DisplayMemberBinding="{Binding EmployeeSalary}" />
<GridViewColumn Header="EmployeeDesigner" Width="150" DisplayMemberBinding="{Binding EmployeeDesigner}" />
<GridViewColumn Header="EmployeeEmailID" Width="150" DisplayMemberBinding="{Binding EmployeeEmailID}" />
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
Employee.cs
public IEnumerable<Employee> SelectedEmployees
{
get
{
selectedEmployees = Employees.Where(o => o.IsSelected).ToList();
return selectedEmployees;
}
set
{
selectedEmployees = value;
OnPropertyChanged("SelectedEmployees");
}
}
public bool IsSelected
{
get
{
//Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("SELE")));
return isSelected;
}
set
{
isSelected = value;
OnPropertyChanged("IsSelected");
}
}
public ObservableCollection<Employee> Employees
{
get
{
return employees;
}
set
{
employees = value;
OnPropertyChanged("Employees");
}
}
public ICollectionView FilteredCollection
{
get
{
return filteredCollection;
}
set
{
filteredCollection = value;
OnPropertyChanged("FilteredCollection");
}
}
public Employee SelectedEmployee
{
get
{
//Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(selectedEmployee.SelectedEmployee.ToString())));
return selectedEmployee;
}
set
{
selectedEmployee = value;
if (selectedEmployee == null)
{
ModeOfExecuting = "Searching / Adding Mode";
OnPropertyChanged("SelectedEmployee");
OnPropertyChanged("ModeOfExecuting");
OnPropertyChanged("FilteredCollection");
}
if (selectedEmployee != null)
{
//MessageBox.Show(Employees[SelectedIndex.GetValueOrDefault()].EmployeeName.ToString());
//List<Employee> FilteredCollectionList = FilteredCollection.Cast<Employee>().ToList();
//MessageBox.Show(FilteredCollectionList[0].EmployeeName);
if (selectedEmployee.EmployeeName != string.Empty)
{
RememberValueEmployeeName = selectedEmployee.EmployeeName;
DynamicSearchEmployeeName = RememberValueEmployeeName;
}
if (selectedEmployee.EmployeeID != null)
{
RememberValueEmployeeID = selectedEmployee.EmployeeID;
DynamicSearchEmployeeID = RememberValueEmployeeID;
}
if (selectedEmployee.EmployeeSalary != null)
{
RememberValueEmployeeSalary = selectedEmployee.EmployeeSalary;
DynamicSearchEmployeeSalary = RememberValueEmployeeSalary.ToString();
}
if (selectedEmployee.EmployeeDesigner != string.Empty)
{
RememberValueEmployeeDesigner = selectedEmployee.EmployeeDesigner;
DynamicSearchEmployeeDesigner = RememberValueEmployeeDesigner;
}
if (selectedEmployee.EmployeeEmailID != string.Empty)
{
RememberValueEmployeeEmailID = selectedEmployee.EmployeeEmailID;
DynamicSearchEmployeeEmailID = RememberValueEmployeeEmailID;
}
ModeOfExecuting = "Editing Mode";
OnPropertyChanged("SelectedEmployee");
}
OnPropertyChanged("SelectedEmployee");
}
}