Cообщество разработчиков на платформе Microsoft >
Форумы
>
Windows Presentation Foundation (WPF)
>
Trigger for textbox and button
Trigger for textbox and button
- 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
Ответы
- 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- Помечено в качестве ответаJim Zhou - MSFTМодератор10 июля 2009 г. 9:37
- 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:The source for DelegateCommand (along with some great walkthroughs) is available on the WPF Futures site under the title 'WPF Model-View-ViewModel Toolkit'.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); }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- Помечено в качестве ответаJim Zhou - MSFTМодератор10 июля 2009 г. 9:37
- Hi raffaeu_bermuda,
here an different solution.
Xaml:
C#:<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>
Hope it helps.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 }
- Помечено в качестве ответаJim Zhou - MSFTМодератор10 июля 2009 г. 9:37
Все ответы
- 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:The source for DelegateCommand (along with some great walkthroughs) is available on the WPF Futures site under the title 'WPF Model-View-ViewModel Toolkit'.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); }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- Помечено в качестве ответаJim Zhou - MSFTМодератор10 июля 2009 г. 9:37
- Hi raffaeu_bermuda,
here an different solution.
Xaml:
C#:<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>
Hope it helps.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 }
- Помечено в качестве ответаJim Zhou - MSFTМодератор10 июля 2009 г. 9:37
- 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 - 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- Помечено в качестве ответаJim Zhou - MSFTМодератор10 июля 2009 г. 9:37
- 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- Снята пометка об ответеJim Zhou - MSFTМодератор10 июля 2009 г. 9:37
- Помечено в качестве ответаJim Zhou - MSFTМодератор10 июля 2009 г. 9:36
- Thanks Raul, this is what I was looking for.
MCAD - MCPD

