在datagrid中添加上下文菜单,如何获取select Item值
我是MVVM模式的WPF编程新手。 现在我在datagrid中添加了上下文菜单。 但是,当我单击鼠标右键时,我不知道如何获取选择的行值。 这是我的xmal
这是我的viewModel
class MainViewModel:NotificationObject { private ObservableCollection storyList; public ObservableCollection StoryList { get { return storyList; } set { storyList = value; this.RaisePropertyChanged("StoryList"); } } public DelegateCommand AddIssueCommand { get; set; } public DelegateCommand BurnDownChartCommand { get; set; } private Story selectStory; public Story SelectStory { get { return selectStory; } set { selectStory = value; this.RaisePropertyChanged("SelectStory"); } } public void LoadStory() { this.storyList = new ObservableCollection(); Story story1 = new Story(){ID=1, Title="win App", StoryPoints=0, StackRank=1, Estimate=40, CompletedWork=10, RemainWork=30, CompletedProcess=25, TodayComments="Coding", YesterdayComments="N/a"}; Story story2 = new Story() { ID = 2, Title = "win App", StoryPoints = 10, Estimate = 40, CompletedWork = 10, RemainWork = 30, CompletedProcess = 25, TodayComments = "Coding 20%", YesterdayComments = "N/a" }; Story story3 = new Story() { ID = 3, Title = "win App", StoryPoints = 10, Estimate = 50, CompletedWork = 20, RemainWork = 30, CompletedProcess = 20, TodayComments = "Coding 30%", YesterdayComments = "N/a" }; Story story4 = new Story() { ID = 4, Title = "win App", StoryPoints = 10, Estimate = 60, CompletedWork = 30, RemainWork = 30, CompletedProcess = 50, TodayComments = "Coding 40%", YesterdayComments = "N/a" }; Story story5 = new Story() { ID = 5, Title = "win App", StoryPoints = 10, Estimate = 40, CompletedWork = 10, RemainWork = 30, CompletedProcess = 25, TodayComments = "Coding 50%", YesterdayComments = "N/a" }; Story story6 = new Story() { ID = 6, Title = "win App", StoryPoints = 10, Estimate = 30, CompletedWork = 30, RemainWork = 0, CompletedProcess = 100, TodayComments = "Coding 60%", YesterdayComments = "N/a" }; storyList.Add(story1); storyList.Add(story3); storyList.Add(story2); storyList.Add(story4); storyList.Add(story5); storyList.Add(story6); } public MainViewModel() { this.SelectStory = new Story(); this.LoadStory(); this.AddIssueCommand = new DelegateCommand(new Action(this.AddIssueCommandExecute)); } public void AddIssueCommandExecute() { if (SelectStory != null) { System.Windows.MessageBox.Show("Add an Issue" + SelectStory.Title + "!"); } else { System.Windows.MessageBox.Show("choose an story first!"); } //System.Windows.MessageBox.Show("record" + RecordIndex); } }![What I need][1]
非常感谢你
这是WPF中的常见问题。 解决方案是利用DataTemplate
项中的Tag
属性来保存数据项。 首先,让我们添加这部分:
...
现在我们可以访问UserControl
的DataContext
,它可以在每个数据对象的Tag
属性中找到,让我们从ContextMenu
绑定到它…我们使用一个名为PlacementTarget
的方便属性来实现:
需要注意的一件事是上面的CommandParameter
显示的YourCollection.CurrentItem
属性。 CurrentItem
属性是我添加到我的集合类中的属性,用于绑定到UI中集合控件的SelectedItem
属性。 如果你没有其中一个,那没关系,但是你需要一个绑定到集合控件的SelectedItem
属性的属性才能使它工作。 在我的例子中,我有这个:
扩展Bolu的注释,您可以使用SelectedItem
来获取当前项目。 以下是一个简单的例子:
SelectedItem
现在绑定到ViewModel中的SelectedItemProperty
。
bigworld12在这里接近正确的答案,但如果您的上下文菜单是模板化的,它会中断。 尝试:
DataGridRow row = ((sender as MenuItem)?.GetAncestors() ?.FirstOrDefault(dpo => dpo.GetType() == typeof(ContextMenu)) as ContextMenu) ?.PlacementTarget as DataGridRow;
对于代码隐藏。 我使用了可空运算符,以防你以某种方式到达这里没有预期的父树和目标(可能是在离开网格时触发上下文菜单。)
我知道这是一个老问题,但我想分享这个非常简单的解决方案
xaml:
代码背后:
上述就是C#学习教程:在datagrid中添加上下文菜单,如何获取select Item值分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
Private Sub MenuItem_Click(sender As Object, e As RoutedEventArgs) Dim row As DataGridRow = DirectCast(DirectCast(DirectCast(sender, MenuItem).GetParentObject, ContextMenu).PlacementTarget, DataGridRow) 'replace with what ever item you want Dim srvr As Server = DirectCast(row.Item, Server) End Sub
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1025829.html