C# 项目选择上的WPF调用函数(使用MVVM范例)

C# 项目选择上的WPF调用函数(使用MVVM范例),c#,wpf,data-binding,mvvm,mvvm-light,C#,Wpf,Data Binding,Mvvm,Mvvm Light,我有一个列表框,它绑定到客户的可观察集合。这方面的XAML代码是: <ListBox x:Name="lb_Customers" Height="683" ItemsSource="{Binding Path=Customers, UpdateSourceTrigger=PropertyChanged}"> <ListBox.ItemTemplate> <DataTemplate> <Label

我有一个列表框,它绑定到客户的
可观察集合
。这方面的XAML代码是:

<ListBox x:Name="lb_Customers" Height="683" ItemsSource="{Binding Path=Customers, UpdateSourceTrigger=PropertyChanged}">
     <ListBox.ItemTemplate>
          <DataTemplate>
               <Label Margin="0,0,0,0" Padding="0,0,0,0" Content="{Binding Name}" />
          </DataTemplate>
      </ListBox.ItemTemplate>
</ListBox>
当我在这个列表框中选择一个客户时,我想执行一些代码来编译客户的订单历史记录

但是,我不知道如何使用数据绑定/命令绑定来实现这一点

我的问题:我从哪里开始?

您可以将“currentlyselected”对象添加到viewmodel中,并将其与listbox的“SelectedItem”属性绑定。然后在“set”访问器中执行所需的操作。

如托蒙德所建议:

改变

<ListBox x:Name="lb_Customers" Height="683" ItemsSource="{Binding Path=Customers, UpdateSourceTrigger=PropertyChanged}">

SelectedValue
SelectedItem
有何不同?尝试绑定
SelectedItem
无效,但我将尝试使用此选项。我很好奇为什么一个比另一个更有效。这个SO线程很好地描述了SelectedValue和SelectedItem之间的差异
<ListBox x:Name="lb_Customers" Height="683" ItemsSource="{Binding Path=Customers, UpdateSourceTrigger=PropertyChanged}">
<ListBox x:Name="lb_Customers" Height="683" ItemsSource="{Binding Path=Customers, UpdateSourceTrigger=PropertyChanged}", SelectedValue="{Binding SelectedCustomer}">
private Customer _selectedCustomer;
public Customer SelectedCustomer
{
    get {}
    set
    {
        if (_selectedCustomer != value)
        {
            Set("SelectedCustomer", ref _selectedCustomer, value);
            this.RaisePropertyChanged("SelectedCustomer");

            // Execute other changes to the VM based on this selection...
        }
    }
}