1

I've bound the textbox to a command using the trigger but nothing appears to happen when the user hits enter on the keyboard. Why would that be? What I want to happen is when the user hits enter it appends the name to the list and clears the input field.

enter image description here

The main areas of focus are here: xaml snippet

   <TextBox Grid.Row="0" Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged}">
            <TextBox.InputBindings>
                <KeyBinding Key="Enter" Command="{Binding Path=CmdSomething, UpdateSourceTrigger=PropertyChanged}"/>
            </TextBox.InputBindings>
        </TextBox>

and its calling this command in the main view model:

 public void CmdSomething()
        {
            Console.WriteLine(InputText);
        }

Below is the code for the entire project just in case someone needs it to test.

PROJECT CODE

VNodes.cs

namespace WpfApplication1
{
    public class VNode
    {
        public string Name { get; set; }
    }
}

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication1">

    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <TextBox Grid.Row="0" Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged}">
            <TextBox.InputBindings>
                <KeyBinding Key="Enter" Command="{Binding Path=CmdSomething, UpdateSourceTrigger=PropertyChanged}"/>
            </TextBox.InputBindings>
        </TextBox>

        <ListBox Grid.Row="1" Background="LightBlue" ItemsSource="{Binding VNodes}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                    </WrapPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

MainViewModel

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.Collections.ObjectModel;


namespace WpfApplication1
{
    public class MainViewModel : ObservableObject
    {
        private ObservableCollection<VNode> _vnodes;
        public ObservableCollection<VNode> VNodes
        {
            get { return _vnodes; }
            set
            {
                _vnodes = value;
                NotifyPropertyChanged("VNodes");
            }
        }

        private string _inputText = "";
        public string InputText
        {
            get { return _inputText; }
            set
            {
                if (value != _inputText)
                {
                    _inputText = value;
                }
            }
        }

        public void CmdSomething()
        {
            Console.WriteLine(InputText);
        }

        public MainViewModel()
        {
            //hard coded data for testing
            VNodes = new ObservableCollection<VNode>();
            List<string> names = new List<string>() { "Tammy", "Doug", "Mike", "Joey", "Leslie", "Emily", "Tom" };

            foreach(string name in names)
            {
                VNode item = new VNode();
                item.Name = name;
                VNodes.Add(item);
            }
        }
    }
}

ObservableObjects.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace WpfApplication1
{
    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
ProgrammingDude
  • 597
  • 1
  • 5
  • 25
JokerMartini
  • 5,674
  • 9
  • 83
  • 193

1 Answers1

2

CmdSomething in your code is not a ICommand but instead just a function.

Now there are two ways to make it work in your case.

Create an ICommand. Wonderful tutorial.

If MyCommand is a property of type ICommand in your ViewModel, then to bind your Custom Command :

<TextBox.InputBindings>
      <KeyBinding Key="Enter" Command="{Binding MyCommand, Mode=OneWay}" />
</TextBox.InputBindings>

And if you want to call your function, then you can use CallMethodAction behavior. Discussion. You can search google for more tutorials.

For your current issue, this works without any issues whatsoever :

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

       <TextBox x:Name="textBox" Margin="144,80,288,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="32">
            <i:Interaction.Triggers>
                <ei:KeyTrigger Key="Return"> <!-- Note Blend Behaviors don't use Enter, instead use Return value -->
                    <ei:CallMethodAction TargetObject="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}" MethodName="CmdSomething"/>
                </ei:KeyTrigger>
            </i:Interaction.Triggers>
        </TextBox>

Good discussion

Community
  • 1
  • 1
AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
  • @JokerMartini have you set CommandTarget="{Binding ElementName=MyTextBox}" – AnjumSKhan Dec 19 '15 at 12:40
  • that still appears to do nothing for some reason. – JokerMartini Dec 19 '15 at 13:46
  • @JokerMartini when you say its not working, tell the error/exception/screenshot. You can use screentogif converter utility. – AnjumSKhan Dec 19 '15 at 15:37
  • Nothing happens when placing the suggested solutions into my code. The function never gets executed – JokerMartini Dec 19 '15 at 16:02
  • 1
    @JokerMartini I will make a sample app and will upload at dropbox in 2 hours from now. – AnjumSKhan Dec 19 '15 at 16:16
  • @JokerMartini Please check https://www.dropbox.com/s/b5g28c6ed726sow/WpfCustomMethod.rar?dl=0, Reference Microsoft.Expression.Interactions.dll, System.Window.Interactivity.dll from C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries – AnjumSKhan Dec 19 '15 at 18:13
  • Yoy are a champ. I'll check this out as soon as I get home from the gym. Thank you very much. – JokerMartini Dec 19 '15 at 18:44