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);
}
Related
i want to send Data from one Textbox on window One to a label on window Two.
starting with window two:
<StackPanel>
<StackPanel x:Name="ButtonStackPanel" Height="Auto" Width="Auto">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<Button Style="{DynamicResource ButtonStyle}" Content="To The Dark Side" Click="OnClickToDarkSide"/>
<Button Style="{DynamicResource ButtonStyle}" Content="To The Gray" Click="OnClickToGraySide"/>
<Button Style="{DynamicResource ButtonStyle}" Content="To The Light Side" Click="OnClickToLightSide"/>
</StackPanel>
<Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock" Content="{Binding Source=CodeText}"/>
<Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Style Window" Name="StyleWindowButton" Click="OnClickOpenStyleWindow"/>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Text Window" Name="TextWindowButton" Click="OnClickOpenTextWindow"/>
</StackPanel>
<Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
<Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
Codebehind of Window Two:
public MainWindow()
{
(App.Current as App).CodeText = _jediCode;
InitializeComponent();
}
private void OnClickToDarkSide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Dark);
(App.Current as App).CodeText = _sithCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickToLightSide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Light);
(App.Current as App).CodeText = _jediCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickToGraySide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Gray);
(App.Current as App).CodeText = _grayCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickOpenStyleWindow(object sender, RoutedEventArgs e)
{
if (StyleWindowButton.IsChecked == true)
{
styleWindow = new StyleWindow();
styleWindow.Show();
}
else
{
styleWindow.Close();
styleWindow = null;
}
}
private void OnClickOpenTextWindow(object sender, RoutedEventArgs e)
{
if (TextWindowButton.IsChecked == true)
{
textWindow = new InputWindow();
textWindow.Show();
}
else
{
textWindow.Close();
textWindow = null;
}
}
}
window one:
<Grid>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="200" TextWrapping="Wrap"
AcceptsReturn="True" AcceptsTab="True" Text="{Binding Path=CodeText, Source={x:Static Application.Current}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Margin="10,10,0,0">
<!-- TODO: trigger change from this textbox to the textblock in mainwindow -->
</TextBox>
</Grid>
code behind of one is empty.
app.xaml.cs:
public string CodeText
{
get => _codeText;
set { _codeText = value; OnPropertyChanged(nameof(CodeText)); }
}
Ok, the current behavior is clicking on one of the buttons (Dark Side, Gray, Light Side) leads to changes in the CodeText Property, which leads to a change of the content of the label of Window Two and the text of TextBox of Window One. Changing the text of the TextBox, changes also the CodeText Property, but does not lead to a change in the label and thats confusing, why does it work the one way, but not the other.
hope you have a hint for me. :) Maybe i missed a trigger or a kind of refresh for the label
bindings in window One and Two are set differently. (window Two does it wrong, Content="{Binding Source=CodeText}" is not valid binding)
in fact, window Two removes the binding by assigning CodeText directly as local value:
theTextBlock.Content = (App.Current as App).CodeText;
you should remove that line, and use the same binding as in window One:
<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock"
Content="{Binding Path=CodeText, Source={x:Static Application.Current}}"/>
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);
}
I have the code in XAML:
<Button Grid.Column="0" Grid.Row="1" x:Name="btnSete" Click="btn_Click">
<ContentControl.Content>
<Viewbox Margin="3">
<TextBlock Text="7"/>
</Viewbox>
</ContentControl.Content>
</Button>
And code behind:
private void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)e.Source;
txbDisplay.Text = btn.Content.ToString();
}
how do I get the value of <TextBlock Text="7"/> in btn_Click?
Based on your XAML, btn.Content should be the instance of the Viewbox. Then the Child property of the Viewbox should be your TextBlock.
So you should be able to do this (inside your button click event handler):
var viewBox = (Viewbox)btn.Content;
var textBlock = (TextBlock)viewBox.Child;
var text = textBlock.Text; // This is the value you were looking for
txbDisplay.Text = text;
Also, just as a helpful note, the <ContentControl.Content> ... </ContentControl.Content> part of your XAML is superfluous. That block could just be written as:
<Button Grid.Column="0" Grid.Row="1" x:Name="btnSete" Click="btn_Click">
<Viewbox Margin="3">
<TextBlock Text="7"/>
</Viewbox>
</Button>
See the "XAML Content Properties" section on this page for more information why that is if you're not sure.
I want to make border and textblock disappear when i click the button and the border and textblock appear when i click the button again.
This is my xaml:
<Border x:Name="descbox" Background="#A6CFC9A8" Height="80" VerticalAlignment="Bottom" Visibility="Visible" Margin="0,-100,0,0">
<ScrollViewer VerticalScrollMode="Auto" Height="auto" Margin="0,0,0.333,0" HorizontalScrollBarVisibility="Visible">
<StackPanel Width="548">
<TextBlock x:Name="desc" Text="{Binding Description}" FontFamily="verdana" FontSize="19" Foreground="#CC000000" TextWrapping="Wrap" Padding="0,10" TextAlignment="Justify" Margin="0,0,10.333,0"/>
</StackPanel>
</ScrollViewer>
</Border>
<Button Content="Button"/>
How to make it in windows phone 8.1?
XAML :
<Button Content="Button" Click="Button_Click"/>
Code :
private void Button_Click(object sender, RoutedEventArgs e)
{
if (descbox.Visibility == Visibility.Visible)
descbox.Visibility = Visibility.Collapsed;
else
descbox.Visibility = Visibility.Visible;
}
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