X3

RedSky

导航

WPF ContextMenu绑定

<Window DataContext="{Binding MyViewModelInstance}">
  <ListBox ItemsSource="{Binding MyDataItems}">
      <ListBox.ContextMenu>
      <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Mode=Self}}">
          <MenuItem Header="刷新" Command="{Binding DataContext.RefreshCommand, RelativeSource={RelativeSource Mode=Self}}"/>
      </ContextMenu>
      </ListBox.ContentMenu>
  </ListBox>
</Window>

ViewModel类:

public class MyViewModel
{
    public ObservableCollection<string> MyDataItems { get; set; }
    public ICommand RefreshCommand { get; set; }  
}

 

posted on 2021-06-09 15:46  HotSky  阅读(469)  评论(0编辑  收藏  举报