Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7670889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:55:30+00:00 2026-05-31T15:55:30+00:00

in general TextBox control TextChanged event is working but in ItemsControl , the TextBox

  • 0

in general TextBox control TextChanged event is working but in ItemsControl, the TextBox TextChanged Event is not fired how can i do this. I trying to do by using following code which I have implemented but not getting result which I want.

So, what am I doing wrong?

View

<Window x:Class="SoniSoft.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ff="clr-namespace:SoniSoft"
        Title="Window1" Height="300" Width="300">

    <Window.DataContext>
                <ff:ViewModels/>
            </Window.DataContext>
    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="38"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    <TextBox Grid.Row="0" ff:TextBoxBehaviour.TextChangedCommand="{Binding TextChanged}"  />

    <ItemsControl Margin="7,0,0,0" Grid.Row="3" ItemsSource="{Binding Path=ViewModelSearchResults}" x:Name="list">
                <ItemsControl.ItemTemplate>
                    <DataTemplate >
                        <Grid>
                            <TextBox  ff:TextBoxBehaviour.TextChangedCommand="{Binding TextChanged}" Text="{Binding Path=CategoryName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="14" FontWeight="Normal" x:Name=" TextBoxCategoryName" />
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </Window>

View Models

class ViewModels :ViewModelBase
            {
                public ObservableCollection<Category> AllCategorys = new ObservableCollection<Category>();
                DatabaseDataContext db = new DatabaseDataContext();
                private ListCollectionView _objViewModelSearchResults;
                public ListCollectionView ViewModelSearchResults
                {
                    get { return _objViewModelSearchResults; }
                    set
                    {
                        _objViewModelSearchResults = value;
                        OnPropertyChanged("ViewModelSearchResults");
                    }
                }
                public ViewModels()
                {
                    AllCategorys.Clear();
                    foreach (var item in db.Categories.OrderBy(c => c.CategoryName))
                    {
                        AllCategorys.Add(item);
                    }
                    ViewModelSearchResults = new ListCollectionView(AllCategorys);
                }
                public ICommand TextChanged
                {
                    get
                    {
                        //  this is very lazy: I should cache the command!
                        return new TextChangedCommand();
                    }
                }
        private class TextChangedCommand : ICommand
                {
                    public event EventHandler CanExecuteChanged;
                    public void Execute(object parameter)
                    {
                        MessageBox.Show("Text Changed");
                    }

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

DependencyProperty

class EventBehaviourFactory
                {
                    public static DependencyProperty CreateCommandExecutionEventBehaviour(RoutedEvent routedEvent, string propertyName, Type ownerType)
                    {
                        DependencyProperty property = DependencyProperty.RegisterAttached(propertyName, typeof(ICommand), ownerType,
                                                                           new PropertyMetadata(null,
                                                                               new ExecuteCommandOnRoutedEventBehaviour(routedEvent).PropertyChangedHandler));

                        return property;
                    }
    private class ExecuteCommandOnRoutedEventBehaviour : ExecuteCommandBehaviour
                    {
                        private readonly RoutedEvent _routedEvent;

                        public ExecuteCommandOnRoutedEventBehaviour(RoutedEvent routedEvent)
                        {
                            _routedEvent = routedEvent;
                        }

                        /// <summary>
                        /// Handles attaching or Detaching Event handlers when a Command is assigned or unassigned
                        /// </summary>
                        /// <param name="sender"></param>
                        /// <param name="oldValue"></param>
                        /// <param name="newValue"></param>
                        protected override void AdjustEventHandlers(DependencyObject sender, object oldValue, object newValue)
                        {
                            UIElement element = sender as UIElement;
                            if (element == null) { return; }

                            if (oldValue != null)
                            {
                                element.RemoveHandler(_routedEvent, new RoutedEventHandler(EventHandler));
                            }

                            if (newValue != null)
                            {
                                element.AddHandler(_routedEvent, new RoutedEventHandler(EventHandler));
                            }
                        }

                        protected void EventHandler(object sender, RoutedEventArgs e)
                        {
                            HandleEvent(sender, e);
                        }
                    }

                    internal abstract class ExecuteCommandBehaviour
                    {
                        protected DependencyProperty _property;
                        protected abstract void AdjustEventHandlers(DependencyObject sender, object oldValue, object newValue);

                        protected void HandleEvent(object sender, EventArgs e)
                        {
                            DependencyObject dp = sender as DependencyObject;
                            if (dp == null)
                            {
                                return;
                            }

                            ICommand command = dp.GetValue(_property) as ICommand;

                            if (command == null)
                            {
                                return;
                            }

                            if (command.CanExecute(e))
                            {
                                command.Execute(e);
                            }
                        }

                        /// <summary>
                        /// Listens for a change in the DependencyProperty that we are assigned to, and
                        /// adjusts the EventHandlers accordingly
                        /// </summary>
                        /// <param name="sender"></param>
                        /// <param name="e"></param>
                        public void PropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs e)
                        {
                            // the first time the property changes,
                            // make a note of which property we are supposed
                            // to be watching
                            if (_property == null)
                            {
                                _property = e.Property;
                            }

                            object oldValue = e.OldValue;
                            object newValue = e.NewValue;

                            AdjustEventHandlers(sender, oldValue, newValue);
                        }
                    }

                }

             class TextBoxBehaviour
                {
                    public static readonly DependencyProperty TextChangedCommand = EventBehaviourFactory.CreateCommandExecutionEventBehaviour(TextBox.TextChangedEvent, "TextChangedCommand", typeof(TextBoxBehaviour));

                    public static void SetTextChangedCommand(DependencyObject o, ICommand value)
                    {
                        o.SetValue(TextChangedCommand, value);
                    }

                    public static ICommand GetTextChangedCommand(DependencyObject o)
                    {
                        return o.GetValue(TextChangedCommand) as ICommand;
                    }
                 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:55:31+00:00Added an answer on May 31, 2026 at 3:55 pm

    Here is the problem. You are setting the command in an ItemTemplate. Thus it is binding to the Category object you have in the ListCollectionView. Now this is the object that doesnt contain any command for your text changed. What does contain the command for your TextChanged is the DataContext of the UserControl and you need to bind it to that.

    Now there are is a way to work around and its called Ancestor RelativeSource. As I work with silverlight it might work different but this line of code should do.

    Edit:

    The actual line should be. this because it is ofcourse a window and you need to have the DataContext (the viewmodel) and then the property TextChanged:

    <TextBox ff:TextBoxBehaviour.TextChangedCommand="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Path=DataContext.TextChanged}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

General Overview: The function below spits out a random ID. I'm using this to
So I've got this control I'm trying to make. It's an in-place text editor
For general protocol message exchange, which can tolerate some packet loss. How much more
In general I think I can convey most programming related concepts quite well. Yet,
I am brand new to jQuery (and really javascript in general) and am trying
I have this code, and it renders this HTML. How can I apply CSS
I am looking for a general, reusable UI pattern I can use for editing
I've faced with a strange problem in ASP.net/SQL Server and really can not find
General Confusion I have bands which can have 3 genres. I read in a
general.css #feedback_bar { /*props*/ } another.css #feedback_bar { /*props*/ } Is this allowed? Will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.