命令未从viewmodel WPF执行 [英] Command is not executing from viewmodel WPF

查看:75
本文介绍了命令未从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 the ClickCommand property on the dcsReportingPeriod type, but the property is defined on the DocumentsViewModel type.

Try resolving the property relative to the TreeView instead:

<Hyperlink Command="{Binding Path=DataContext.ClickCommand, ElementName=trvMenu}">


这篇关于命令未从viewmodel WPF执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆