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

In this part of the code is the event TextChanged to enable the button in te applicationbar.
C#:
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;
}
XAML:
<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="
" 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="
" 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="
" FontSize="21.333" Height="63" VerticalAlignment="Bottom"/>

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:
ApplicationBar Class
PhoneApplicationPage.ApplicationBar Property
How to change app bar icon buttons and menu items dynamically

Related

Right-Click on Dynamically created RadioButtons

On my application I am able to dynamically create Radio Buttons and I need to have the ability to add a right click to each dynamically created button.
For an example button I added the below code would have the right click as needed.
XAML:
<RadioButton Content="3001" x:Name="myButton" Width="75" Height="40" FontSize="25" ContextRequested="MyButton_ContextRequested"/>
C# Code Behind:
private void ShowMenu(bool isTransient)
{
FlyoutShowOptions myOption = new FlyoutShowOptions();
myOption.ShowMode = isTransient ? FlyoutShowMode.Transient : FlyoutShowMode.Standard;
CommandBarFlyout1.ShowAt(myButton, myOption);
}
private void MyButton_ContextRequested(Windows.UI.Xaml.UIElement sender, ContextRequestedEventArgs args)
{
// always show a context menu in standard mode
ShowMenu(true);
}
Dynamic Button Add -
XAML:
<StackPanel x:Name="SiteAddPanel" Orientation="Horizontal" VerticalAlignment="Top">
<Button PointerEntered="Button_PointerEntered" PointerExited="Button_PointerExited" Width="80" Height="45">
<muxc:AnimatedIcon x:Name="SearchAnimatedIcon">
<muxc:AnimatedIcon.Source>
<animatedvisuals:AnimatedFindVisualSource/>
</muxc:AnimatedIcon.Source>
</muxc:AnimatedIcon>
<Button.Flyout>
<Flyout
x:Name="flyoutSiteID">
<StackPanel Orientation="Horizontal">
<TextBox
x:Name="seachSiteID"
Header="Enter the 4 Digit Site Number"
PlaceholderText="SiteID"
>
<TextBox.KeyboardAccelerators>
<KeyboardAccelerator Key="Enter" Invoked="KeyboardAccelerator_Invoked"/>
</TextBox.KeyboardAccelerators>
</TextBox>
<Button VerticalAlignment="Bottom" Click="Add_Site">
<SymbolIcon Symbol="Add">
</SymbolIcon>
<!--<Button.KeyboardAccelerators>
<KeyboardAccelerator Key="Enter"/>
</Button.KeyboardAccelerators>-->
</Button>
</StackPanel>
</Flyout>
</Button.Flyout>
</Button>
<AppBarSeparator Width="20"/>
</StackPanel>
C# Code Behind:
private void Add_Site(object sender, RoutedEventArgs e)
{
var btnName = "btnSite" + seachSiteID.Text;
if (SiteAddPanel.Children.Count <= 6)
{
RadioButton button = new RadioButton();
button.Name = btnName;
button.Width = 75;
button.Height = 40;
button.FontSize = 25;
button.Content = seachSiteID.Text;
SiteAddPanel.Children.Add(button);
seachSiteID.Text = "";
flyoutSiteID.Hide();
}
}
I have been trying to add MyButton_ContextRequested to each one of the dynamic buttons and haven't had much luck.
This is what the menu bar looks like:
MenuBar

Button in a different file than the main

I have 2 button but they need to be in a different file than the mainWindow.cs.
I can't figure how to do that.
So the Button_Click_2 must be in the ReadData.cs and the
Button_Click_3 must be in the WriteData.cs
The app don't recognize the button when they are not in the mainWindow.
How can I do that ?
ReadData.cs :
public new void Button_Click_2(object sender, RoutedEventArgs e)
{
string text = verifyCard("5"); // 5 - is the block we are reading
textBlock1.Text = text.ToString();
}
MainWindow.xaml.cs :
public void Button_Click_2(object sender, RoutedEventArgs e)
{
//When I click it don't detected the code in the ReadData.cs
// The code of the button must be in the ReadData.cs
}
MainWindow.xaml :
<Grid>
<Button Content="Connexion" HorizontalAlignment="Left"
Margin="265,142,0,0" VerticalAlignment="Top" Width="236" Height="44"
Click="Button_Click_1"/>
<Button Content="Lire donnée carte" HorizontalAlignment="Left"
Margin="265,276,0,0" VerticalAlignment="Top" Width="236" Height="42"
Click="Button_Click_2"/>
<Button Content="Ecrire donnée carte" HorizontalAlignment="Left"
Margin="265,344,0,0" VerticalAlignment="Top" Width="236" Height="41"
Click="Button_Click_3"/>
<TextBox Name="textBox1" HorizontalAlignment="Left" Height="23"
Margin="321,224,0,0" TextWrapping="Wrap" Text="TextBox"
VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
<TextBlock Name="textBlock1" HorizontalAlignment="Left"
Margin="291,95,0,0" TextWrapping="Wrap" VerticalAlignment="Top"
Height="23" Width="177"/>
</Grid>
It must be possible to forced the app to detected and execute the code in another file than the Main.
I'm stuck... do any of you have the solution ?
You can't just place the button code in ReadData.cs class. as the event is related to ui of MainWindow.xaml create an object for ReadData do something like this
public new void Button_Click_2(object sender, RoutedEventArgs e)
{
ReadData rd= new ReadData();
string text = rd.verifyCard("5");
textBlock1.Text = text.ToString();
}

Edit HyperLink NavigationURI WPF

I'm new to WPF.
I have a ComboBox with multiple values and a HyperLink, whenever the ComboBox value changes, I want to change the NavigateUri of the HyperLink accordingly.
In the cs file, I have a dictionary, where the keys are the same as the combo items, and the value of each key is the link I want to navigate to according to the ComboBox selection.
LinkQuery["A"] = "https://google.com";
LinkQuery["B"] = "https://facebook.com";
LinkQuery["C"] = "https://Youtube.com";
<ComboBox x:Name="box_ComboBox" Visibility="Visible" Grid.Column="5" Grid.Row="4" Width="90"
ItemsSource="{Binding Path=Fields}"
IsSynchronizedWithCurrentItem="True"
SelectedValue="{Binding Path=Field}" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" SelectionChanged="component_ComboBox_SelectionChanged"/>
....
<TextBlock x:Name="LinkToQuery" Grid.Row="39" Grid.Column="1" Grid.ColumnSpan="4" Margin="10">
<Hyperlink x:Name="URLQuery" RequestNavigate="Hyperlink_RequestNavigate" Foreground="Blue">
Selected: A
</Hyperlink>
</TextBlock>
And the cs file:
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(e.Uri.AbsoluteUri);
e.Handled = true;
}
private void component_ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
string selectedComponent = box_ComboBox.SelectedItem.ToString();
LinkToQuery.Text = string.Format("Selected: {0} ", box_ComboBox.SelectedItem.ToString());
URLQuery.NavigateUri = new System.Uri(LinkQuery[selectedComponent],System.UriKind.Absolute);
}
When I change the Combo selection, the text does change properly, but the link does not work.
Thank you.
Put a Run element inside the Hyperlink and set the Text property of this one:
<TextBlock x:Name="LinkToQuery" Grid.Row="39" Grid.Column="1" Grid.ColumnSpan="4" Margin="10">
<Hyperlink x:Name="URLQuery" RequestNavigate="Hyperlink_RequestNavigate" Foreground="Blue">
<Run x:Name="linkText" Text="Selected: A" />
</Hyperlink>
</TextBlock>
private void component_ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
string selectedComponent = box_ComboBox.SelectedItem.ToString();
linkText.Text = string.Format("Selected: {0} ", selectedComponent);
URLQuery.NavigateUri = new System.Uri(LinkQuery[selectedComponent], System.UriKind.Absolute);
}

Get value from one textbox to another XAML

I have created a text box which when focused brings up a popup box with other text box's as options.. and when pressed closes the popup box.. but I'd like that to then take the value from whichever text box I have selected and add into the original text box. Is this possible? I'm new to wpf so forgive me!
Here is my code so far.
<Grid>
<StackPanel Margin="0,51,0,-51">
<TextBox x:Name="text" GotKeyboardFocus="text_GotKeyboardFocus" Margin="158,0,169,0" Height="24" Text="Select Your Time..." />
<Popup x:Name="popup" Width="282" Height="300" PlacementTarget="{Binding ElementName=text}">
<Grid>
<StackPanel>
<TextBox x:Name="text2" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="1 second" />
<TextBox x:Name="text3" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="2 second" />
<TextBox x:Name="text4" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="3 second" />
<TextBox x:Name="text5" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="4 second" />
<TextBox x:Name="text6" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="5 second" />
</StackPanel>
</Grid>
</Popup>
</StackPanel>
</Grid>
private void text_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
popup.IsOpen = true;
}
private void text_GotKeyboardFocus2(object sender, RoutedEventArgs e)
{
popup.IsOpen = false;
}
Get value from one textbox to another in xaml:
<TextBox Name="txtBox1"/>
<TextBox Name="txtBox2" Text="{Binding ElementName=txtBox1, Path=Text,UpdateSourceTrigger=PropertyChanged}" />
I hope I got your point.
Just add text.Text = (sender as TextBox).Text; to your second event handler as follows :
private void text_GotKeyboardFocus2(object sender, RoutedEventArgs e)
{
popup.IsOpen = false;
text.Text = (sender as TextBox).Text;
}
Good luck.
Get value from one textbox to another XAML
<TextBox Name="txtBox1"/>
<TextBox Name="txtBox2" Text="{Binding ElementName=txtBox1, Path=Text,UpdateSourceTrigger=PropertyChanged}" />

Hyperlinks in TextBlock in windows phone 8.0 using C#

how to generate hyperlink from text writeen in textbox to textblock in windows phone 8.0 using C#
ex:- i entered
www.google.com in textbox and clicked on button after button click
the result should be
www.google.com
with hyperlink in textblock
You can easily put Hyperlink into RichTextBlock (in WP8.1 Runtime). I've also put Run in hyperlink so it's easier to manage its content. Example:
<StackPanel>
<TextBox Name="myTextBox" Width="200"/>
<RichTextBlock TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center">
<Paragraph>
<Run Text="This is a link to google:"/>
<LineBreak/>
<Hyperlink x:Name="myhyperlink" Click="myhyperlink_Click">
<Run x:Name="hyperText" Text="textInside"/>
</Hyperlink>
<LineBreak/>
<Run Text="you can click it to invoke doEvent in your code."/>
</Paragraph>
</RichTextBlock>
</StackPanel>
In the code behind - some logic example:
public MainPage()
{
this.InitializeComponent();
myTextBox.TextChanged += (sender, e) => hyperText.Text = myTextBox.Text;
}
private async void myhyperlink_Click(Windows.UI.Xaml.Documents.Hyperlink sender, Windows.UI.Xaml.Documents.HyperlinkClickEventArgs args)
{
await Windows.System.Launcher.LaunchUriAsync(new Uri(#"http://" + myTextBox.Text));
}
Note that in WP8.0 and WP8.1 Silverlight you will have to use RichTextBox with IsReadOnly = true
Use a HyperlinkButton control.
<HyperlinkButton NavigateUri="http://www.google.com">
<HyperlinkButton.Content>
<TextBlock Text="google.com" />
</HyperlinkButton.Content>
</HyperlinkButton>
try this:
XAML:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0">
<StackPanel x:Name="stack">
<TextBox x:Name="txtInput"></TextBox>
<Button Content="Create Link" Click="Button_Click"/>
</StackPanel>
</Grid>
CS:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (txtInput.Text != "")
{
HyperlinkButton obj = new HyperlinkButton();
obj.NavigateUri = new Uri(txtInput.Text,UriKind.RelativeOrAbsolute);
obj.Content = txtInput.Text;
obj.TargetName = "_blank";
this.stack.Children.Add(obj);
}
}
for e.g. try with http://google.com in textbox
Try this
xaml
<StackPanel x:Name="stack">
<TextBlock Text="{Binding LineThree}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBox x:Name="txtInput"></TextBox>
<Button Content="Create Link" Click="Button_Click"/>
<RichTextBox x:Name="textBox" ></RichTextBox>
</StackPanel>
and the button click in the cs file
private void Button_Click(object sender, RoutedEventArgs e)
{
Hyperlink hyperlink = new Hyperlink();
hyperlink.Inlines.Add(txtInput.Text);
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(hyperlink);
textBox.Blocks.Add(myParagraph);
}

Categories

Resources