命令未从viewmodel WPF执行 [英] Command is not executing from viewmodel WPF
本文介绍了命令未从viewmodel WPF执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
点击超链接,不通过委托命令执行Viewmodel的Click()方法。
Datacontext是:
DataContext = new DocumentsViewModel();
我在这里缺少什么,请帮助我。
我尝试了什么:
TreeView:
< TreeView Name =trvMenuItemsSource ={Binding ReportPeriods} >
< TreeView.ItemTemplate>
< HierarchicalDataTemplate DataType ={x:Type self:dcsReportingPeriod}ItemsSource ={Binding Path = ChildReports}>
< TextBlock Text ={Binding Name}/>
< HierarchicalDataTemplate.ItemTemplate>
< DataTemplate>
< TextBlock>
< Hyperlink Command ={Binding ClickCommand}>
< TextBlock Text ={Binding Path = Name}>
< TextBlock.ContextMenu>
< ContextMenu>
< MenuItem Header =PDF>< / MenuItem>
< / ContextMenu>
< /TextBlock.ContextMenu>
< / TextBlock>
< / Hyperlink>
< / TextBlock>
< / DataTemplate>
< /HierarchicalDataTemplate.ItemTemplate>
< / HierarchicalDataTemplate>
< /TreeView.ItemTemplate>
< / TreeView>
ViewModel:
公共类DocumentsViewModel:NotificationObject
{
private ObservableCollection< dcsReportingPeriod> _reportPeriods;
public ObservableCollection< dcsReportingPeriod> ReportPeriods
{
get {return _reportPeriods; }
设置
{
_reportPeriods = value;
RaisePropertyChanged(ReportPeriods);
ClickCommand.RaiseCanExecuteChanged();
}
}
public DelegateCommand ClickCommand {get;组; }
public DocumentsViewModel()
{
ClickCommand = new DelegateCommand(Click,ClickCanExecute);
ReportPeriods = new ObservableCollection< dcsReportingPeriod>();
ReportPeriods = null;
}
public bool ClickCanExecute()
{
return ReportPeriods!= null;
}
public void点击()
{
GeodeAssistant.OpenDocument(https://www.google.co.in,true,DocumentNavigationModes.NONE,null,GeodeLaunchModes 。对);
}
}
解决方案
如果检查Visual Studio输出窗口,你会看到很多绑定错误。那是因为你试图绑定到dcsReportingPeriod
类型的ClickCommand
属性,但属性是在DocumentsViewModel
类型。
尝试解析相对于TreeView的属性
相反:
< 超链接 命令 = {Binding Path = DataContext.ClickCommand,ElementName = trvMenu} >
Click on hyperlink not executing Click() method of Viewmodel through delegate command. Datacontext is: DataContext = new DocumentsViewModel(); What I am missing here, please help me.
What I have tried:
TreeView:
<TreeView Name="trvMenu" ItemsSource="{Binding ReportPeriods}"> <TreeView.ItemTemplate> <HierarchicalDataTemplate DataType="{x:Type self:dcsReportingPeriod}" ItemsSource="{Binding Path=ChildReports}"> <TextBlock Text="{Binding Name}" /> <HierarchicalDataTemplate.ItemTemplate> <DataTemplate> <TextBlock> <Hyperlink Command="{Binding ClickCommand}"> <TextBlock Text="{Binding Path=Name}" > <TextBlock.ContextMenu> <ContextMenu> <MenuItem Header ="PDF" ></MenuItem> </ContextMenu> </TextBlock.ContextMenu> </TextBlock> </Hyperlink> </TextBlock> </DataTemplate> </HierarchicalDataTemplate.ItemTemplate> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>
ViewModel:
public class DocumentsViewModel : NotificationObject { private ObservableCollection<dcsReportingPeriod> _reportPeriods; public ObservableCollection<dcsReportingPeriod> ReportPeriods { get { return _reportPeriods; } set { _reportPeriods = value; RaisePropertyChanged("ReportPeriods"); ClickCommand.RaiseCanExecuteChanged(); } } public DelegateCommand ClickCommand { get; set; } public DocumentsViewModel() { ClickCommand = new DelegateCommand(Click, ClickCanExecute); ReportPeriods = new ObservableCollection<dcsReportingPeriod>(); ReportPeriods = null; } public bool ClickCanExecute() { return ReportPeriods != null; } public void Click() { GeodeAssistant.OpenDocument("https://www.google.co.in", true, DocumentNavigationModes.NONE, null, GeodeLaunchModes.Right); } }
解决方案
If you check the Visual Studio output window, you'll see lots of binding errors. That's because you're trying to bind to theClickCommand
property on thedcsReportingPeriod
type, but the property is defined on theDocumentsViewModel
type.
Try resolving the property relative to theTreeView
instead:
<Hyperlink Command="{Binding Path=DataContext.ClickCommand, ElementName=trvMenu}">
这篇关于命令未从viewmodel WPF执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文