none
How to trigger click event within datatemplate using by delegatecommand

    Question

  • Hi,

    I'm working in datagrid, in that, one column value consists with hyperlinkbutton. The logic behind is, if i click the hyperlinkbutton inside the grid, that page should navigate to another page with some parameter value using by delegatecommand pattern.

    For example, in the datagrid i have department name as link button. If click "IT" department, the page should navigate to another page which shows the employee details who belongs this particular deparment.

    Please help me on this?

     

    Thanks

    Ram

     

    Friday, March 25, 2011 6:00 AM

Answers

  • I got the soultion !!!, thanks a lot Cool

    Now I can able to get the click event in viewmodel using by delegate command. Next step is, if click the link it should navigate to another page with parameter value as querystring.

    For this, can we able to achieve using eventaggregation? If yes, please let me know some examples.

     

    Thanks

    Ram

     

    Monday, March 28, 2011 5:48 AM
  • I can't clear about this statement which one you mentioned. Please let me know in detail.

    Here is ViewDiscovery for static but in your case create controller then use View Injection

    public class HelloWorldModule : IModule
        {
            private readonly IRegionViewRegistry regionViewRegistry;

            public HelloWorldModule(IRegionViewRegistry registry)
            {
                this.regionViewRegistry = registry;  
            }

            public void Initialize()
            {
                regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(Views.NView));
            }
        }

    Friday, April 01, 2011 5:33 AM

All replies

  • Hi, Is that ViewModel?

    Friday, March 25, 2011 6:32 AM
  • Yes, Its based on MVVM.

    Thanks for your quick response.

    I given my code as sample, please look on it. In below coding, i have one hyperlinkbutton "hyper1". If I click this button event should fire with content value as parameter using by delegatecommand.

    In MainPage.xaml

            <sdk:DataGrid AutoGenerateColumns="False" Grid.Row="1" HorizontalAlignment="Left" Name="Deparment" VerticalAlignment="Top" SelectionMode="Single" ItemsSource="{Binding Path=Departments}" >
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTextColumn Header="DeptNo" Binding="{Binding DeptNo}" IsReadOnly="True" Width="*"></sdk:DataGridTextColumn>
                    <sdk:DataGridTextColumn Header="Dept Name" Binding="{Binding DeptName}" IsReadOnly="True" Width="*"></sdk:DataGridTextColumn>
                    <sdk:DataGridTemplateColumn Width="*">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate  x:Name="templ">
                                <StackPanel x:Name="sp1">
                                    <HyperlinkButton Name="hyper1" Content="{Binding DeptName}" cmd:Click.Command="{Binding ExitCommand}" cmd:Click.CommandParameter="{Binding Content, ElementName=hyper1}"></HyperlinkButton>
                                </StackPanel>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                </sdk:DataGrid.Columns>
            </sdk:DataGrid> 

    In ShellViewModel.cs:

    public class ApplicationShellViewModel : INotifyPropertyChanged
        {
            public DelegateCommand<object> ExitCommand { get; private set; }
            public string strName { get; set; }

            public ApplicationShellViewModel()
            {
                ExitCommand = new DelegateCommand<object>(Save,CanSave);
            }

            private void Save(object args)
            {
                this.strName = args.ToString();
            }

            private bool CanSave(object args)
            {
                return true;
            }

            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string pName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(pName));
                }
            } 

     
    Friday, March 25, 2011 8:23 AM
  • Try this:

    <UserControl.Resources>
        <app:VM x:Key="VM" />
    </UserControl.Resources>


     

    cmd:Click.Command="{Binding ExitCommand, Source={StaticResource VM}}"


     

    BTW, are you using SL4? If yes you don't need "cmd:Click.".

    Friday, March 25, 2011 9:20 AM
  • I got the soultion !!!, thanks a lot Cool

    Now I can able to get the click event in viewmodel using by delegate command. Next step is, if click the link it should navigate to another page with parameter value as querystring.

    For this, can we able to achieve using eventaggregation? If yes, please let me know some examples.

     

    Thanks

    Ram

     

    Monday, March 28, 2011 5:48 AM
  • Its based on NavigationContext, but its tightly coupled.

    But I have to choose eventaggregation pattern for my task. I'm expecting the same functionality should be in eventaggregation (its a loosely coupled).

     

    Monday, March 28, 2011 7:56 AM
  • Do you mean Prism?

    Monday, March 28, 2011 7:58 AM
  • Hi,

    event aggregation not for naviagation.

    Monday, March 28, 2011 9:06 AM
  • But I have to choose eventaggregation pattern for my task.

    Why?

    Monday, March 28, 2011 1:44 PM
  • NavigationContext - Tight Coupling

    EventAggregator - Loose Coupling, and also need to implement as per requirment.

    Requirement is, if

    Thursday, March 31, 2011 2:38 AM
  • NavigationContext - Tight Coupling

    EventAggregator - Loose Coupling, and also need to implement as per requirment.

    Requirement is, if i click the

    Thursday, March 31, 2011 2:38 AM
  • NavigationContext - Tight Coupling

    EventAggregator - Loose Coupling, and also need to implement as per requirment.

    Requirement is, if i click the hyperlink in datagrid, page should navigate to another page with parameter value.

    Thursday, March 31, 2011 2:39 AM
  • Hi,

    How did you implement your bootstraper Unity or Mef?

    Thursday, March 31, 2011 3:04 AM
  • Unity

    use Region attach property

    <ContentControl prism:RegionManager.RegionName="MainRegion" ... />

    Thursday, March 31, 2011 3:08 AM
  • Yes, we are using by Unity.

    Thursday, March 31, 2011 6:58 AM
  • I can't clear about this statement which one you mentioned. Please let me know in detail.

    Thursday, March 31, 2011 9:36 AM
  • I can't clear about this statement which one you mentioned. Please let me know in detail.

    Here is ViewDiscovery for static but in your case create controller then use View Injection

    public class HelloWorldModule : IModule
        {
            private readonly IRegionViewRegistry regionViewRegistry;

            public HelloWorldModule(IRegionViewRegistry registry)
            {
                this.regionViewRegistry = registry;  
            }

            public void Initialize()
            {
                regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(Views.NView));
            }
        }

    Friday, April 01, 2011 5:33 AM
  • Hi Obuli,

    Thanks for mark as answered because Prism has many step and difficult for me to expain to you.

    Friday, April 01, 2011 9:35 AM
  • Hi

    I'm facing another issue when the viewmodel constructor is parameterized.

    Using the below code in xaml file:

    <UserControl.Resources>
     <local:ViewModel x:Key="VM"></local:ViewModel>
    </UserControl.Resources>

    Assign the staticresource to hyperlinkbutton:

    <HyperlinkButton x:Name="link" VerticalAlignment="Bottom" Content="TELEVISION" Command="{Binding GetNewPageCommand,Source={StaticResource VM}}"></HyperlinkButton>

    In viewmodel.cs file, i defined the below coding for delegatecommand:

    public ViewModel(
     IUnityContainer unityContainer,
     IBusyService busyService,
     IEventAggregator eventAggregator ) : base(eventAggregator, busyService, unityContainer) {
     this.GetNewPageCommand = new DelegateCommand<object>(handleEventcommand);
    }

    public void handleEventcommand(object obj)
    {
     MessageBox.Show("hi");
    }

     

    Problem is, while executing it says

    No matching constructor found on type 'ViewModel'. [Line: 11 Position: 45]

     I hope the reason behind, its not supporting <UserControl.Resource> when we are using parameterized constructor.

    If I use with default constructor the staticresource is working.

    Please let me know the solution when using parameterized constructor for delegatecommand.

    Thanks in advance

     
    Monday, April 04, 2011 8:54 AM
  • Hi,

    You should not remove default construtor just add addition constructor.

    Apparently parameter mismatch.

    I guess, that seem you are create navigation in viewModel.

    Monday, April 04, 2011 9:14 AM
  • Hi,

    Thanks, Its working !!!

    Yes, you are correct. I'm implementing the page navigation in viewmodel using by eventaggregator.

    If i click the hyperlink it should be navigate to another page using by eventaggregator. Then, do i need to use two different regions with different screename ? if its how is the approach?

    Monday, April 04, 2011 10:30 AM
  • If i click the hyperlink it should be navigate to another page using by eventaggregator. Then, do i need to use two different regions with different screename ? if its how is the approach?

    Could you elaborate above? How do you populate Hyperlink static or dynamic?

    You can use Basic Region Navigation + based on URIs and an extensible navigation mechanism.

    Tuesday, April 05, 2011 6:54 AM
  • Heperlink based on dynamic one.

    I declared default constructor as below:

    public ChannelProfitViewModel( )
    {
             this.GetNewPageCommand = new DelegateCommand<object>(handleEventcommand);
    }

    Followed by above for accessing click command from xaml file

    <UserControl.Resources>
            <local:ChannelProfitViewModel x:Key="VM"></local:ChannelProfitViewModel>
    </UserControl.Resources>

    <HyperlinkButton x:Name="link"  VerticalAlignment="Bottom" Content="TELEVISION" Command="{Binding GetNewPageCommand,Source={StaticResource VM}}" CommandParameter="CLIENT_PROFITABILITY"></HyperlinkButton>

    Using these, i can able to achieve the click functionality. But getting the issue when navigation using eventaggregator. Why bcs, the IEventAggregator not initialized and getting null object error. If you use the below constuctor you will not get this error.

    public ChannelProfitViewModel(
     IUnityContainer unityContainer,
     IBusyService busyService,
     IEventAggregator eventAggregator ) : base(eventAggregator, busyService, unityContainer)

    {

    }

    If you executing the application, after click the hyperlink button it triggered default constructor and fire the event with error like “null object defined”. The reason is, as per this framework it should call the parameterized constructor instead of default one. That is the issue we are facing now.

     Either we need to find a way intsead of using "<UserContro.Resource>" in xaml for initialize Delegatecommand using StaticResource.

    Thanks in advance. 

     
    Wednesday, April 06, 2011 3:06 AM
  • Hi,

    If your viewModel take some parameter you should not instantiate object via XAML.

    Create in code.

    public ViewConstructor(ViewModel viewModel)
            {
                this.InitializeComponent();

                // Set the ViewModel as this view's data context.
                this.DataContext = viewModel;
            }

    You can subscribe to event aggregator service then navigate at controller part.

    Wednesday, April 06, 2011 3:50 AM
  • Hi,

    I think, That is not good idea for create navigation on many ViewModel. Create at controller part.

    Thursday, April 07, 2011 5:20 AM
  • Hi,

    I achieved the click event through eventhandler.

    Now the event is firing, but i couldnt able to pass the parameter value to other page.

    I'm using the below code to call the clientprofitscreen page after eventaggregator published using container.resolve

    this.Screen = this.Container.Resolve<ClientProfitScreen>();

     

    In that ClientProfitScreen.cs file, i assigning the values to datacontext.

    public ClientProfitScreen(IUnityContainer container)
                : base(container)
            {  
                this.ViewModel = Container.Resolve<IClientProfitViewModel>();
                this.View = Container.Resolve<ClientProfitView>();
                this.View.DataContext = this.ViewModel;
            }

     In this page I need to get the parameter value, how can we achieve this?

    Thanks in advance

     

     
    Sunday, April 10, 2011 5:57 AM
  • Hi,

    If you are using Event Aggregation for create navigation, There has a way is retrieve from Region Context.

    Region context. Use this to provide contextual information between the host and views in the host's region. This approach is somewhat similar to the DataContext, but it does not rely on it.

    Sunday, April 10, 2011 6:41 AM