当前位置: 首页 > 编程语言 > C#

如何给datagrid添加上下文菜单,如何获取selectItem的值分享

时间:2023-04-10 17:53:14 C#

为datagrid添加上下文菜单,如何获取selectItem的值MVVM模式。现在我在数据网格中添加了上下文菜单。但是,我不知道如何在右键单击时获取所选行的值。这是我的xmal这是我的viewModelclassMainViewModel:NotificationObject{privateObservableCollectionstoryList;publicObservableCollectionStoryList{get{返回storyList;}设置{故事列表=值;this.RaisePropertyChanged("StoryList");}}publicDelegateCommandAddIssueCommand{get;放;}publicDelegateCommandBurnDownChartCommand{get;放;私人故事选择故事;publicStorySelectStory{get{returnselectStory;}设置{selectStory=值;this.RaisePropertyChanged("SelectStory");}}publicvoidLoadStory(){this.storyList=newObservableCollection();故事story1=newStory(){ID=1,Title="winApp",StoryPoints=0,StackRank=1,Estimate=40,CompletedWork=10,RemainWork=30,CompletedProcess=25,TodayComments="Coding",YesterdayComments="不适用"};Storystory2=newStory(){ID=2,Title="winApp",StoryPoints=10,Estimate=40,CompletedWork=10,RemainWork=30,CompletedProcess=25,今天Comments="Coding20%",YesterdayComments="N/a"};Storystory3=newStory(){ID=3,Title="winApp",StoryPoints=10,Estimate=50,CompletedWork=20,RemainWork=30,CompletedProcess=20,TodayComments="Coding30%",YesterdayComments="不适用"};Storystory4=newStory(){ID=4,Title="winApp",StoryPoints=10,Estimate=60,CompletedWork=30,RemainWork=30,CompletedProcess=50,TodayComments="Coding40%",YesterdayComments="不适用"};Storystory5=newStory(){ID=5,Title="winApp",StoryPoints=10,Estimate=40,CompletedWork=10,RemainWork=30,CompletedProcess=25,TodayComments="Coding50%",YesterdayComments="不适用"};Storystory6=newStory(){ID=6,Title="winApp",StoryPoints=10,Estimate=30,CompletedWork=30,RemainWork=0,CompletedProcess=100,TodayComments="Coding60%",YesterdayComments="不适用"};storyList.Add(story1);storyList.Add(story3);storyList.Add(story2);故事List.Add(故事4);storyList.Add(story5);storyList.Add(story6);}publicMainViewModel(){this.SelectStory=newStory();这个.LoadStory();this.AddIssueCommand=newDelegateCommand(新动作(this.AddIssueCommandExecute));}publicvoidAddIssueCommandExecute(){if(SelectStory!=null){System.Windows.MessageBox.Show("添加问题"+SelectStory.Title+"!");}else{System.Windows.MessageBox.Show("先选择一个故事!");}//System.Windows.MessageBox.Show("记录"+RecordIndex);}}![我需要什么][1]非常感谢WPF中的一个常见问题解决方案是使用DataTemplate项中的Tag属性来保存数据项。首先,让我们添加这部分:...现在我们可以访问UserControl的DataContext,它可以在每个数据对象的Tag属性中找到,让我们从ContextMenu绑定到它...我们使用一个名为PlacementTarget的方便属性要实现:...需要注意的是上面的CommandParameter显示的YourCollection.CurrentItem属性。CurrentItem属性是我添加到我的集合类中的一个属性,用于绑定到UI中集合控件的SelectedItem属性。如果您没有其中之一,也没关系,但您需要一个绑定到Collection控件的SelectedItem属性的属性才能使其工作。在我的例子中,我有这个:扩展Bolu的注释,您可以使用SelectedItem来获取当前项目。这是一个简单的示例:SelectedItem现在绑定到ViewModel中的SelectedItemProperty。bigworld12在这里接近正确答案,但如果您的上下文菜单是模板化的,它就会中断。尝试:DataGridRowrow=((senderasMenuItem)?.GetAncestors()?.FirstOrDefault(dpo=>dpo.GetType()==typeof(ContextMenu))asContextMenu)?.PlacementTargetasDataGridRow;对于代码隐藏。我使用了可空运算符,以防你以某种方式到达这里而没有预期的父树和目标(可能在离开网格时触发上下文菜单。)我知道这是一个老问题,但我想分享这个非常简单的解决方案xaml:背后代码:以上就是C#学习教程:如何在datagrid中添加上下文菜单,如何获取selectItem值分享的全部内容,如果对大家有用还需要详细了解C#学习教程,希望大家多多关注——PrivateSubMenuItem_Click(senderAsObject,eAsRoutedEventArgs)DimrowAsDataGridRow=DirectCast(DirectCast(DirectCast(sender,MenuItem).GetParentObject,ContextMenu).PlacementTarget,DataGridRow)'替换成什么itemyouwantDimsrvrAsServer=DirectCast(row.Item,Server)EndSub本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: