Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
        }
}
Posted
Updated 8-May-17 8:22am
v2

1 solution

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:
XAML
<Hyperlink Command="{Binding Path=DataContext.ClickCommand, ElementName=trvMenu}">
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900