Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListView和ListBox中绑定对象/属性之间的差异_C#_Linq_Listview_Listbox_Datatemplate - Fatal编程技术网

C# ListView和ListBox中绑定对象/属性之间的差异

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&

我有一个列表视图,显示员工列表,每个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>
                    <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