Trigger for textbox and buttonHi guys I have a simple search textbox with a related search button.<br/>I would like to enable/disable the search button only if the textbox.text has a lenght grater then 0.<br/>If the user wipe the text I want to re-disable the button.<br/>Thank you<hr class="sig">MCAD - MCPD© 2009 Microsoft Corporation. All rights reserved.Fri, 10 Jul 2009 14:34:14 Za5729464-ae52-45d5-b4fb-29e1a1046a98http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#a5729464-ae52-45d5-b4fb-29e1a1046a98http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#a5729464-ae52-45d5-b4fb-29e1a1046a98raffaeu_bermudahttp://social.msdn.microsoft.com/Profile/en-US/?user=raffaeu_bermudaTrigger for textbox and buttonHi guys I have a simple search textbox with a related search button.<br/>I would like to enable/disable the search button only if the textbox.text has a lenght grater then 0.<br/>If the user wipe the text I want to re-disable the button.<br/>Thank you<hr class="sig">MCAD - MCPDFri, 03 Jul 2009 20:27:09 Z2009-07-03T20:27:09Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#534a3aae-75ab-45c0-ad9a-72fdbaec6caehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#534a3aae-75ab-45c0-ad9a-72fdbaec6caeJamesChambershttp://social.msdn.microsoft.com/Profile/en-US/?user=JamesChambersTrigger for textbox and buttonHello Raffaeu_Bermuda, <div><br/></div> <div>Have you looked into writing an ICommand property?</div> <div><br/></div> <div>I have been using the DelegateCommand from the WPF toolkit lately.  This makes things very easy.</div> <div><br/>All you do is have a property in your code of type 'ICommand'.  Keep a local reference to the command as a member-level variable.  Expose a getter that either returns the ICommand (if it exists) or creates and returns it.</div> <div><br/></div> <div>Here's a snippet from some code I'm working on:</div> <div> <pre lang="x-c#"> private DelegateCommand contactProspectCommand; public ICommand ContactProspectCommand { get { if (contactProspectCommand == null) contactProspectCommand = new DelegateCommand(ContactProspect, CanContactProspect); return contactProspectCommand; } } private void ContactProspect() { bool result = true; result = _updateMethod(prospect); Contacted = result; } private bool CanContactProspect() { return (!prospect.Contacted &amp; prospect.Representative == _representative); }</pre> The source for DelegateCommand (along with some great walkthroughs) is available on the <a href="http://www.codeplex.com/wpf/Release/ProjectReleases.aspx?ReleaseId=14962">WPF Futures</a> site under the title 'WPF Model-View-ViewModel Toolkit'.</div> <div><br/></div> <div>That download will walk you through binding your button to a similar property as above...your CanXxxExecute method would test the length of the string and report.  At that point, the button automatically resets itself with changes to the text.</div> <div><br/></div> <div>Hth!</div> <div><br/>Cheers,</div> <div>-jc</div><hr class="sig">Me, coding and stuff: <a href="http://theycallmemrjames.blogspot.com/">Mr. James</a>Fri, 03 Jul 2009 22:01:07 Z2009-07-03T22:01:07Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#a37fd890-6828-4212-a3eb-a0a98a6e8b45http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#a37fd890-6828-4212-a3eb-a0a98a6e8b45Guenter Schwaigerhttp://social.msdn.microsoft.com/Profile/en-US/?user=Guenter%20SchwaigerTrigger for textbox and buttonHi raffaeu_bermuda,<br/><br/>here an different solution.<br/><br/>Xaml:<br/><br/> <pre lang=x-xml>&lt;Window x:Class=&quot;WpfApplication1.Window19&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; Title=&quot;Window19&quot; Height=&quot;300&quot; Width=&quot;300&quot;&gt; &lt;StackPanel Orientation=&quot;Horizontal&quot;&gt; &lt;TextBox Width=&quot;150&quot; Height=&quot;20&quot; KeyUp=&quot;TextBox_KeyUp&quot; /&gt; &lt;Button Height=&quot;20&quot; IsEnabled=&quot;{Binding Enabled}&quot;&gt;Seach&lt;/Button&gt; &lt;/StackPanel&gt; &lt;/Window&gt;</pre> C#:<br/> <pre lang="x-c#"> public partial class Window19 : Window, INotifyPropertyChanged { private string _searchText; public Window19() { InitializeComponent(); this.DataContext = this; } private void TextBox_KeyUp(object sender, KeyEventArgs e) { TextBox textBox = (TextBox)sender; _searchText = textBox.Text; this.OnPropertyChanged(&quot;Enabled&quot;); } public bool Enabled { get { return !string.IsNullOrEmpty(_searchText); } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } #endregion }</pre> Hope it helps.Fri, 03 Jul 2009 22:05:28 Z2009-07-03T22:05:28Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#5465cc59-f665-43ba-870e-96baf558b5c3http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#5465cc59-f665-43ba-870e-96baf558b5c3raffaeu_bermudahttp://social.msdn.microsoft.com/Profile/en-US/?user=raffaeu_bermudaTrigger for textbox and buttonThey are both great solution but I am working with the MVVM and I do not want to touch the code behind of a view.<br/>Is there any way to build everything with a trigger in XAML?<hr class="sig">MCAD - MCPDSat, 04 Jul 2009 17:12:36 Z2009-07-04T17:12:36Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#8a22f0c8-6fe6-4142-a86b-a3ebd3f92fe2http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#8a22f0c8-6fe6-4142-a86b-a3ebd3f92fe2Rahul P Nathhttp://social.msdn.microsoft.com/Profile/en-US/?user=Rahul%20P%20NathTrigger for textbox and buttonHi,<br/> See if this helps you<br/> <pre lang=x-xml> &lt;Grid&gt; &lt;TextBox Height=&quot;29&quot; Margin=&quot;39,42,62,0&quot; Name=&quot;TextBox1&quot; VerticalAlignment=&quot;Top&quot; /&gt; &lt;Button Height=&quot;23&quot; Margin=&quot;124,0,79,97&quot; Name=&quot;Button1&quot; VerticalAlignment=&quot;Bottom&quot;&gt;Button &lt;Button.Style&gt; &lt;Style&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding=&quot;{Binding ElementName=TextBox1,Path=Text.Length}&quot; Value=&quot;0&quot;&gt; &lt;Setter Property=&quot;Button.IsEnabled&quot; Value=&quot;False&quot;&gt;&lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Button.Style&gt; &lt;/Button&gt; &lt;/Grid&gt;</pre> <br/> Hope it helps<hr class="sig">FEAR NOT TO BE JUST Please mark posts as answers/helpful if it answers your querySun, 05 Jul 2009 10:32:38 Z2009-07-05T10:32:38Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#63504cab-b0c8-4249-b8e2-32ba461fd7dchttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#63504cab-b0c8-4249-b8e2-32ba461fd7dcJamesChambershttp://social.msdn.microsoft.com/Profile/en-US/?user=JamesChambersTrigger for textbox and buttonHello, <div><br/></div> <div>The ICommand solution is actually from the MVVM toolkit.  The code I posted would ideally go in your model.  When you set your view's data context to the model, you can then bind to the command as suggested above.</div> <div><br/>The XAML bits work as well and is another approach whenever you'd rather not have to track a change in the model.  If you think you'll need to examine the text in the box at somepoint it is worth tying your model to that box in MVVM fashion.  At that point, you wouldn't have much at all to do to get an ICommand up and running.</div> <div><br/>Cheers,</div> <div>-jc</div><hr class="sig">Me, coding and stuff: <a href="http://theycallmemrjames.blogspot.com/">Mr. James</a>Thu, 09 Jul 2009 13:25:57 Z2009-07-09T13:25:57Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#5ca6f63d-7531-4d94-99b1-8ae96f0df27chttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a5729464-ae52-45d5-b4fb-29e1a1046a98#5ca6f63d-7531-4d94-99b1-8ae96f0df27craffaeu_bermudahttp://social.msdn.microsoft.com/Profile/en-US/?user=raffaeu_bermudaTrigger for textbox and buttonThanks Raul, this is what I was looking for.<br/><hr class="sig">MCAD - MCPDThu, 09 Jul 2009 13:28:38 Z2009-07-09T13:28:38Z