Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello there. On click of a textblock, I want to lift its contents in my view model and if accurate, navigate to another page. Unfortunately for me, the click event never triggers its bound command. Below is my code

What I have tried:

In the xaml, I have this markup
HTML
<UserControl x:Class="SchoolPrism.Login.Views.Login"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:SchoolPrism.Login.Views"
             xmlns:prism="http://prismlibrary.com/"
             xmlns:constants="clr-namespace:Infrastructure.Constants;assembly=SchoolPrismInfrastructure"
             xmlns:login="clr-namespace:SchoolPrism.Login.ViewModel"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <!-- when we have a view model, bind to that instead-->
    <Grid DataContext="{Binding login:LoginViewModel}">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="5*"/>
        </Grid.RowDefinitions>

        <TextBlock DataContext="{Binding}" Text="{Binding FormResults}"></TextBlock>

        <TextBlock Text="hi, can you see me?" x:Name="SchoolCode" Grid.RowSpan="2" Margin="0,50,0,0">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewMouseDown">
                    
<!--tried replacing interactivity with prism here but it had no effect -->
<i:InvokeCommandAction Command="{Binding submitForm}" CommandParameter="{Binding Text, ElementName=SchoolCode}" ></i:InvokeCommandAction>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBlock>
    </Grid>
</UserControl>


Then, in the view model, I have this

C#
using Prism.Commands;
using Prism.Modularity;
using SchoolPrism.Modules.Login;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SchoolPrism.Login.ViewModel
{
    class LoginViewModel
    {

        ICommand submitForm
        {
            get { return new DelegateCommand<string>(SubmitForm); }
        }

        public void SubmitForm(string code)
        {

            // interact with model over given code

            // if auth, get module instance and invoke the following
            new LoginModule().proceedToNext("AllTopics");

        }
    }
}


Finally, in the module proper, I have


C#
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using Prism.Commands;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using SchoolPrism.Login.Views;
using System;
using System.Windows;
using System.Windows.Input;

namespace SchoolPrism.Modules.Login
{
    [Module(ModuleName = "LoginModule")]
    class LoginModule : BindableBase, IModule
    {

        IRegionManager _regionMan;

        // this method is called on our behalf by prism
        public void Initialize ()
        {

            _regionMan = ServiceLocator.Current.GetInstance<iregionmanager>();

        }

        public void proceedToNext (string target)
        {

            _regionMan.RequestNavigate("LoginForm", new Uri(target, UriKind.Relative)/*, (NavigationResult result) => { return code.Length > 0; }*/);
        }
        }
 }

Please, what am I doing wrong?
Posted
Updated 15-Jul-18 3:50am
v2
Comments
George Swan 15-Jul-18 7:39am    
Try defining your command property like this:
public DelegateCommand<string> SubmitFormCommand { get; private set; }
Then instantiate it in the viewmodel's constructor.
SubmitFormCommand = new DelegateCommand<string>(SubmitForm);
nmeri17 15-Jul-18 7:50am    
That wasn't the cure

Using

HTML
<Grid.DataContext>
  <login:LoginViewModel>    </login:LoginViewModel>
</Grid.DataContext>


Solved it
 
Share this answer
 

Try setting the DataContext like this


C#
<UserControl.Resources>
        <login:LoginViewModel x:Key="loginvm" />
    </UserControl.Resources>
    <UserControl.DataContext>
        <Binding Source="{StaticResource loginvm}" />
</UserControl.DataContext>
 
Share this answer
 
Comments
nmeri17 16-Jul-18 7:13am    
This is also another way to go about it. However, I usually prefer defining all my resources in one file specifically for accessibility application wide. This method will come in handy assuming I intend using the same data context on more than one control. All the same, thanks for stopping by

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