64568

Disable/Enable applicationbar Button in runtime with event textchanged (Windows Phone)

Question:

In this part of the code is the event TextChanged to enable the button in te applicationbar.

<strong>C#:</strong>

private void Textbox_TextChanged(object sender, EventArgs e) { ApplicationBarIconButton btn_guardar = ApplicationBar.Buttons[0] as applicationBarIconButton; if (!string.IsNullOrEmpty(txt_nom_usuario.Text) && !string.IsNullOrEmpty(txt_edad_usuario.Text) && !string.IsNullOrEmpty(txt_peso_usuario.Text)) { btn_guardar.IsEnabled = true; } else btn_guardar.IsEnabled = false; }

<strong>XAML:</strong>

<phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar Mode="Default" IsVisible="True"> <shell:ApplicationBarIconButton x:Name="btn_guardar" IconUri="/icons/appbar.save.rest.png" Text="Guardar" Click="btn_guardar_Click" IsEnabled="False" /> <shell:ApplicationBarIconButton x:Name="btn_atras" IconUri="/icons/appbar.back.rest.png" Text="Atrás" Click="btn_atras_Click" /> </shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar> <TextBlock x:Name="lbl_ingresanombre" Height="39" Margin="60,28,0,0" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Width="248" FontSize="29.333" FontFamily="{StaticResource Helvetica}"><Run Text="Ingresa "/><Run Text="tu nombre"/></TextBlock> <TextBox x:Name="txt_nom_usuario" Height="63" Margin="47,58,69,0" TextWrapping="Wrap" Text="&#xa;" FontSize="21.333" VerticalAlignment="Top" IsEnabled="True" /> <TextBlock x:Name="lbl_edad" Height="38" Margin="60,117,0,0" TextWrapping="Wrap" Text="Ingresa tu edad" VerticalAlignment="Top" FontSize="29.333" HorizontalAlignment="Left" FontFamily="{StaticResource Helvetica}"/> <TextBox x:Name="txt_edad_usuario" InputScope="TelephoneLocalNumber" Height="63" TextWrapping="Wrap" Text="&#xa;" FontSize="21.333" Margin="47,147,69,0" VerticalAlignment="Top" MaxLength="3" /> <TextBlock x:Name="lbl_peso" Height="42" Margin="60,0,0,178" TextWrapping="Wrap" Text="Peso" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="74" FontSize="29.333" d:LayoutOverrides="HorizontalAlignment" FontFamily="{StaticResource Helvetica}"/> <TextBox x:Name="txt_peso_usuario" InputScope="TelephoneLocalNumber" Margin="47,0,69,125" TextWrapping="Wrap" Text="&#xa;" FontSize="21.333" Height="63" VerticalAlignment="Bottom"/>

Answer1:

The application bar doesn't support some basic features when it is set in XAML. You'll have to create the bar and buttons and/or menu items through code.

Here's an example how you can create the bar and add controls to it. The controls can then be accessed later from code:

//button var appBarButton = new ApplicationBarIconButton { IconUri = new Uri("/Images/YourImage.png", UriKind.Relative), Text = "click me" }; appBarButton.Click += new EventHandler(appBarButton_Click); //menu item ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem { Text = "a menu item" } appBarMenuItem.Click += new EventHandler(appBarMenuItem_Click); //application bar //Note that this is not a variable declaration //'ApplicationBar' is a property of 'PhoneApplicationPage' ApplicationBar = new ApplicationBar(); ApplicationBar.Buttons.Add(appBarButton); ApplicationBar.MenuItems.Add(appBarMenuItem); //the events private void appBarButton_Click(object sender, EventArgs e) { } private void appBarMenuItem_Click(object sender, EventArgs e) { }

When all this is done, you've created your own ApplicationBar through code. Now you can change the properties from code, like this:

var theButton = (ApplicationBarIconButton)ApplicationBar.Buttons[0]; if(someCondition) { theButton.IsEnabled = true; } else { theButton.IsEnabled = false; } //or shorter: theButton.IsEnabled = someCondition

This is just an example. In the TextChanged events you can also access the ApplicationBar controls. In these events you can place above code to change the ApplicationBarButton. Hope this clears things up for you! More reading and info:

<ul><li><a href="http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.shell.applicationbar" rel="nofollow"><strong>ApplicationBar Class</strong></a></li> <li><a href="http://msdn.microsoft.com/en-US/library/windowsphone/develop/microsoft.phone.controls.phoneapplicationpage.applicationbar%28v=vs.105%29.aspx" rel="nofollow"><strong>PhoneApplicationPage.ApplicationBar Property</strong></a></li> <li><a href="http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394044%28v=vs.105%29.aspx" rel="nofollow"><strong>How to change app bar icon buttons and menu items dynamically</strong></a></li> </ul>

Recommend

  • verify if username exist to show the database or sho table to field
  • Slow code in “inner joins” lists in python
  • issues with jQuery Validation Plugin and input File
  • eVRC smart cards
  • Text classification extract tags from text
  • How to pass a custom object in REST webservice
  • What's a better way to swap two argument values?
  • Remove every nth element from swift array
  • How to add plugin dependencies to pom.xml
  • WPF: Resize item size to have all items visible
  • Get count of created entries for each day
  • Io exception: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=168821248)(ERR=12505)(ERROR_STACK=(ERROR=
  • Routing in WCF data services
  • ELMAH: Can you set it up to email errors only remotely?
  • Create a multiple screen android application
  • Initialization section of the package
  • How to load Q library with Require.js?
  • OpenGL - Object Transformations and VBOs
  • PHP + XML - how to rename and delete XML elements using SimpleXML or DOMDocument?
  • Responsive design method for collapsing a div
  • How to draw a line dynamically in android [duplicate]
  • Xamarin MonoAndroid Azure mobile service InsertAsync
  • Floated image with variable width and heading with background image
  • Looking for good analogy/examples for monitor verses semaphore
  • Sending HTML Form Data to Spring REST Web Service
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Authentication in Play! and RestEasy
  • jQuery ready not fired after rails link_to is clicked
  • How to have background script and something similar to a default popup?
  • Bad request using file_get_contents for PUT request in PHP
  • Python urlparse: small issue
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • jquery mobile loadPage not working
  • GridView Sorting works once only
  • Data Validation Drop Down Box Arrow Disappearing
  • Matrix multiplication with MKL
  • How do you join a server to an Active Directory (domain)?
  • sending mail using smtp is too slow
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs