我正在使用 MVVM 开发 WPF 应用程序,需要一些帮助。
对于我的 AddressesViewModel 的视图,我有一个带有列表视图的用户控件。我想从 listViewItem 的上下文菜单中执行在 AddressesViewModel 中找到的命令。因为我正在打开一个 ContextMenu,所以在可视化树中找不到它(我在某处读过)。
这是视图标记:
<UserControl>
<ListView Name="lstAddress"
ItemsSource="{Binding Path=Addresses}"
HorizontalAlignment="Stretch" MinHeight="150" MinWidth="100">
<ListView.Resources>
<ContextMenu x:Key="ItemContextMenu">
<MenuItem Header="Add Address"/>
</ContextMenu>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Address" DisplayMemberBinding="{Binding Path=Address}/>
<GridViewColumn Header="City" DisplayMemberBinding="{Binding Path=City}"/>
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</UserControl>
这是 ViewModel 类:
public class AddressesViewModel
{
public List<AddressViewModel> Addresses { get; set; }
public ICommand AddAddressCommand { get; set; }
}
public class AddressViewModel
{
public String Address { get; set; }
public String City { get; set; }
public String State { get; set; }
public String Zip { get; set; }
}
提前致谢