Edit HyperLink NavigationURI WPF - c#

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);
}

Related

How to get Window.UI.Color from Windows.UI.Colors?

I have a combobox to select the background color of a given application object. I'm filling the combobox with the code below, but I don't know how to retrieve the selected value to change the background color of the other object. Any help is most welcome. Thanks.
XAML
<ComboBox x:Name="cbxBackgroundColor" SelectionChanged="cbxBackgroundColor_SelectionChanged" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Name}" Width="32" Height="32" VerticalAlignment="Center" />
<TextBlock Text="{Binding Name}" Margin="10, 0, 0, 0" VerticalAlignment="Center" FontSize="12"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Code Behind
public VideoPage()
{
this.InitializeComponent();
cbxBackgroundColor.ItemsSource = typeof(Colors).GetProperties();
}
private void cbxBackgroundColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Code to set MyObject.Background with cbxBackgroundColor.SelectedItem
}
The following code works in wpf.
private void cbxBackgroundColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Color selectedColor = (Color)(cbxBackgroundColor.SelectedItem as PropertyInfo).GetValue(null, null);
this.Background = new SolidColorBrush(selectedColor);
or
MyObject.Background = new SolidColorBrush(selectedColor);
}

c# searching in textbox from listbox

I have an textbox and listbox with persons names. I want to type the name in the textbox and it should update the Listbox information. But I don't know how to do it. How should I do it?
I would like to filter the listbox rows when something is written in the textbox.
MainWindow.xaml code:
<ListBox HorizontalAlignment="Left" Height="127" ItemsSource="{Binding Persons}" Name="PersonLstbox"
Margin="10,22,0,0" VerticalAlignment="Top" Width="197">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding FirstName}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Textbox code:
<TextBox Name="searchpersonbx" HorizontalAlignment="Left" Height="23" Margin="420,150,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="searchpersonbx_TextChanged"/>
MainWindow.xaml.cs code:
private void searchpersonbx_TextChanged(object sender, TextChangedEventArgs e)
{
}
you can modify your code as below:-
Here I have used StartsWith() to get all the strings in specified order
your user name list
List<string> userName = new List<string>();
TextChanged Event
private void searchpersonbx_TextChanged(object sender, TextChangedEventArgs e)
{
string text = searchpersonbx.Text;
List<string> filteredUserName = userName.Select(x => x.StartsWith(text)).ToList();
listBox.ItemsSource = filteredUserName;
}

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);
}

ComboBox ItemSource ObservationCollection

I have a combo box that have item source that bind to observation collection and with Data-template as the item-template. I have a problem when i click at the combo box .value and it doesn't show the value of the selected value in the combo box
Below is my Data Template:
<DataTemplate x:Key="ComboBoxTemplate">
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding ColorCode}" Width="30" Height="15"/>
<TextBlock Text="{Binding ColorName}" Margin="5,0,0,0"/>
</StackPanel>
</DataTemplate>
Below is my combo box:
<ComboBox Name="cmbAccentColors" Grid.Column="1" Width="130" Height="20"
ItemsSource="{Binding Source={StaticResource ComboColorData}}"
ItemTemplate="{StaticResource ComboBoxTemplate}"
IsSynchronizedWithCurrentItem="True" MaxDropDownHeight="120"
SelectionChanged="cmbColors_SelectionChanged"
>
Private void cmbColors_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (!IsLoaded)
{
return;
}
MessageBox.Show(cmbAccentColors.SelectedItem.ToString());
}
Add DisplayMemberPath to your combobox. DisplayMemberPath specifies the path.
DisplayMemberPath = "YourProperty";
Also this how you access the selected Item,
Private void cmbColors_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
string text = (sender as ComboBox).SelectedItem.Text;
}

GridView is Not Loading Data

I'm trying to load data into a GridView after a TextBlock from another GridView on the page has been tapped/clicked. The first GridView containing the list of TextBlocks loads correctly.
Here is my XAML code for both GridViews, my Bindings seem to be correct:
<GridView x:Name="CourseNoGridView" Margin="50,50,0,0" Grid.Row="1" VerticalAlignment="Top" Height="568" ItemsSource="{Binding Distinct_CourseNo}" SelectionMode="Single" Padding="0,0,0,10" HorizontalAlignment="Left" Width="525" SelectionChanged="CourseNoGridView_SelectionChanged">
<GridView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="White">
<TextBlock x:Name="CourseNoTextBlock" Text="{Binding CourseNo}" TextWrapping="NoWrap" FontSize="24" Width="200" Height="Auto" Padding="10" Tapped="CourseNoTextBlock_Tapped"/>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
<GridView x:Name="SectionsGridView" Margin="580,50,0,0" Grid.Row="1" VerticalAlignment="Top" Height="568" ItemsSource="{Binding Clicked_CourseNo_Sections}" SelectionMode="Single" Padding="0,0,0,10" HorizontalAlignment="Left" Width="776" SelectionChanged="CourseNoGridView_SelectionChanged">
<GridView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="White">
<TextBlock x:Name="SectionTextBlock" Text="{Binding Get_Section}" TextWrapping="NoWrap" FontSize="24" Width="200" Height="Auto" Padding="10"/>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
Here is my code for handling the clicking/tapping of an item in the first GridView:
private void CourseNoGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
clickedSection = (Sections)e.AddedItems[0];
}
private void CourseNoTextBlock_Tapped(object sender, TappedRoutedEventArgs e)
{
this.Clicked_CourseNo_Sections = (from s in Roster_Sections
where s.CourseNo.Equals(clickedSection.CourseNo)
select s).ToList();
}
What you want to do is use an ObservableCollection and bind your your Grid View to this. Then in your "Tapped" event handler you clear the existing items from this collection and add the new items.
Something like this:
private readonly ObservableCollection<Sections> currentSections = new ObservableCollection<Sections>();
//This is what we bind to
public ObservableCollection<Sections> CurrentSections { get { return currentSections; } }
private void CourseNoGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
clickedSection = (Sections)e.AddedItems[0];
}
private void CourseNoTextBlock_Tapped(object sender, TappedRoutedEventArgs e)
{
var courseSections = (from s in Roster_Sections
where s.CourseNo.Equals(clickedSection.CourseNo)
select s);
CurrentSections.Clear();
CurrentSections.AddRange(courseSections);
}
There's some documentation here:
http://msdn.microsoft.com/en-us/library/windows/apps/hh758320.aspx
It seems like adding the last line of code below fixed the problem.
private void CourseNoTextBlock_Tapped(object sender, TappedRoutedEventArgs e)
{
this.Clicked_CourseNo_Sections = (from s in Roster_Sections
where s.CourseNo.Equals(clickedSection.CourseNo)
select s).ToList();
SectionsGridView.ItemsSource = Clicked_CourseNo_Sections;
}

Categories

Resources