Odeslat dotazOdeslat dotaz
 

OdpovědětTrigger for textbox and button

  • 3. července 2009 20:27raffaeu_bermuda Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    Hi guys I have a simple search textbox with a related search button.
    I would like to enable/disable the search button only if the textbox.text has a lenght grater then 0.
    If the user wipe the text I want to re-disable the button.
    Thank you
    MCAD - MCPD

Odpovědi

  • 5. července 2009 10:32Rahul P Nath Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     OdpovědětObsahuje kód
    Hi,
    See if this helps you
     <Grid>
            <TextBox Height="29" Margin="39,42,62,0" Name="TextBox1" VerticalAlignment="Top" />
            <Button  Height="23" Margin="124,0,79,97" Name="Button1" VerticalAlignment="Bottom">Button
                <Button.Style>
                    <Style>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=TextBox1,Path=Text.Length}" Value="0">
                                <Setter Property="Button.IsEnabled"  Value="False"></Setter>
                            </DataTrigger> 
                        </Style.Triggers>  
                    </Style> 
                </Button.Style>
            </Button>
        </Grid>
    

    Hope it helps
    FEAR NOT TO BE JUST Please mark posts as answers/helpful if it answers your query
  • 3. července 2009 22:01JamesChambers Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     OdpovědětObsahuje kód
    Hello Raffaeu_Bermuda,

    Have you looked into writing an ICommand property?

    I have been using the DelegateCommand from the WPF toolkit lately.  This makes things very easy.

    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.

    Here's a snippet from some code I'm working on:
            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 & prospect.Representative == _representative);
            }
    
    The source for DelegateCommand (along with some great walkthroughs) is available on the WPF Futures site under the title 'WPF Model-View-ViewModel Toolkit'.

    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.

    Hth!

    Cheers,
    -jc

    Me, coding and stuff: Mr. James
  • 3. července 2009 22:05Guenter Schwaiger Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     OdpovědětObsahuje kód
    Hi raffaeu_bermuda,

    here an different solution.

    Xaml:

    <Window x:Class="WpfApplication1.Window19"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window19" Height="300" Width="300">
    	<StackPanel Orientation="Horizontal">
    		<TextBox Width="150" Height="20"
    				  KeyUp="TextBox_KeyUp" />
    		<Button Height="20"
    				IsEnabled="{Binding Enabled}">Seach</Button>
    	</StackPanel>
    </Window>
    
    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("Enabled");
    		}
    
    		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
    		
    	}
    
    Hope it helps.

Všechny reakce

  • 3. července 2009 22:01JamesChambers Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     OdpovědětObsahuje kód
    Hello Raffaeu_Bermuda,

    Have you looked into writing an ICommand property?

    I have been using the DelegateCommand from the WPF toolkit lately.  This makes things very easy.

    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.

    Here's a snippet from some code I'm working on:
            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 & prospect.Representative == _representative);
            }
    
    The source for DelegateCommand (along with some great walkthroughs) is available on the WPF Futures site under the title 'WPF Model-View-ViewModel Toolkit'.

    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.

    Hth!

    Cheers,
    -jc

    Me, coding and stuff: Mr. James
  • 3. července 2009 22:05Guenter Schwaiger Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     OdpovědětObsahuje kód
    Hi raffaeu_bermuda,

    here an different solution.

    Xaml:

    <Window x:Class="WpfApplication1.Window19"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window19" Height="300" Width="300">
    	<StackPanel Orientation="Horizontal">
    		<TextBox Width="150" Height="20"
    				  KeyUp="TextBox_KeyUp" />
    		<Button Height="20"
    				IsEnabled="{Binding Enabled}">Seach</Button>
    	</StackPanel>
    </Window>
    
    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("Enabled");
    		}
    
    		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
    		
    	}
    
    Hope it helps.
  • 4. července 2009 17:12raffaeu_bermuda Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    They are both great solution but I am working with the MVVM and I do not want to touch the code behind of a view.
    Is there any way to build everything with a trigger in XAML?
    MCAD - MCPD
  • 5. července 2009 10:32Rahul P Nath Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     OdpovědětObsahuje kód
    Hi,
    See if this helps you
     <Grid>
            <TextBox Height="29" Margin="39,42,62,0" Name="TextBox1" VerticalAlignment="Top" />
            <Button  Height="23" Margin="124,0,79,97" Name="Button1" VerticalAlignment="Bottom">Button
                <Button.Style>
                    <Style>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=TextBox1,Path=Text.Length}" Value="0">
                                <Setter Property="Button.IsEnabled"  Value="False"></Setter>
                            </DataTrigger> 
                        </Style.Triggers>  
                    </Style> 
                </Button.Style>
            </Button>
        </Grid>
    

    Hope it helps
    FEAR NOT TO BE JUST Please mark posts as answers/helpful if it answers your query
  • 9. července 2009 13:25JamesChambers Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    Hello,

    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.

    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.

    Cheers,
    -jc

    Me, coding and stuff: Mr. James
  • 9. července 2009 13:28raffaeu_bermuda Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    Thanks Raul, this is what I was looking for.

    MCAD - MCPD