Click here to Skip to main content
15,916,601 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
To simplify, Criticized for writing a novel in another forum w/no code a month ago, I made a quick Wpf project (uses MVVM) with 2 buttons on the UI. When a button is clicked, I need my ViewModelBase and/or ParameterCommand classes, to know which UI Button was clicked. So I can route the Speech Synthesizer to the correct Text to Speak. Thanks 4 any help!!

<Window x:Class="Wpf_School_Announce.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Wpf_School_Announce"
        xmlns:vm="clr-namespace:Wpf_School_Announce.ViewModels"
        mc:Ignorable="d"
        Title="Announcements" Height="236.436" Width="293.218">
    <Window.Resources>
        <vm:ViewModelBase x:Key="viewModel"/>
    </Window.Resources>
    <Grid DataContext="{Binding Source=viewModel}">
        <StackPanel Margin="0,10">
            <Button x:Name="btn1stBell" Content="1st Bell" HorizontalAlignment="Center" VerticalAlignment="Top" Width="75" Margin="0,10" 
                     Command="{Binding ParameterCommand, Source={StaticResource viewModel}}" 
                    CommandParameter="{Binding Command, ElementName=btn1stBell}"/>
            <Button x:Name="btnLunchMenu" Content="Lunch Menu" HorizontalAlignment="Center" VerticalAlignment="Top" Width="75" Margin="0,10"
                     Command="{Binding ParameterCommand, Source={StaticResource viewModel}}" 
                    CommandParameter="{Binding Command, ElementName=LunchMenu}"/>
        </StackPanel>
    </Grid>
</Window>


namespace Wpf_School_Announce.ViewModels
{
    public class ViewModelBase
    {
        public ParameterCommand ParameterCommand { get; set; }

        public ViewModelBase()
        {
            ParameterCommand = new ParameterCommand(this);
        }

        public void ParameterMethod(string <Not sure what needs to go here>)
        {
           Debug.WriteLine("Parameter Comand:{0}", AnnoucementModel);
            //Todo: Need to find out which UI button was clicked to direct The Speech Synthesozer to the correct Speech Text.
        }
    }
}

namespace Wpf_School_Announce.ViewModels.Commands
{
    public class ParameterCommand : ICommand
    {
        public ViewModelBase ViewModel { get; set; }

        public ParameterCommand(ViewModelBase viewModel)
        {
            ViewModel = viewModel;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            ViewModel.ParameterMethod(parameter as String);
        }
    }
}


What I have tried:

I have tried using CommandParameter="{Binding Source={StaticSelf ElementName}}"
Posted
Updated 8-Dec-16 12:29pm
v3
Comments
Richard Deeming 12-Nov-16 10:16am    
It's not entirely clear what you're trying to achieve, but you should be able to bind the SelectedItem of the list control to a property on your VM, and pass a CommandParameter to the button's command to determine which action to take.

None of that would require knowing the name of the button, or giving the VM access to the view.

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



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