C# ListView和ListBox中绑定对象/属性之间的差异
我有一个列表视图,显示员工列表,每个SelectionChanged事件在一组文本框中显示每个员工的不同信息C# ListView和ListBox中绑定对象/属性之间的差异,c#,linq,listview,listbox,datatemplate,C#,Linq,Listview,Listbox,Datatemplate,我有一个列表视图,显示员工列表,每个SelectionChanged事件在一组文本框中显示每个员工的不同信息 <ListView ItemsSource="{Binding Employees}" x:Name="lvEmployeeList" Grid.Row="1" Grid.Column="1" Width="385" SelectedIndex="0" Margin="1,1,1,1" > <i:Interaction.Triggers&
<ListView ItemsSource="{Binding Employees}" x:Name="lvEmployeeList" Grid.Row="1" Grid.Column="1" Width="385" SelectedIndex="0" Margin="1,1,1,1" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding ProcessCommand}" CommandParameter="{Binding ElementName=lvEmployeeList, Path=SelectedItem.EmployeeID}" ></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
你能告诉我是什么导致了这个错误吗?在ListView中没有问题,但在ListBox中我得到了空对象。我能够通过在viewModel中添加选定的属性来克服这个问题
public Employee SelectedEmployee { get; set; }
然后以OneWayToSource模式将SelectedItem绑定到该属性
最后,在ProcessCommand执行中,检索您的员工id并从中执行任何操作
var id = SelectedEmployee.EmployeeId;
// Do whatever you want
希望这有帮助
public Employee SelectedEmployee { get; set; }
<ListBox x:Name="lstEmployees" ItemsSource="{Binding Employees}" SelectionMode="Single" SelectedItem="{Binding Path=SelectedEmployee, Mode=OneWayToSource}">
<i:InvokeCommandAction Command="{Binding ProcessCommand}"/>
var id = SelectedEmployee.EmployeeId;
// Do whatever you want