c# searching in textbox from listbox - c#

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

Related

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

Setting Display position of column in DataGrid

I'm generating a Hyperlink button in the code behind. Problem is the column appears first in the datagrid despite setting the property to DisplayIndex=4 the column appears first and then the rest of the columns. How can I fix the order of this column?
XAML Code
<sdk:DataGrid AutoGenerateColumns="True" HorizontalAlignment="Left" Height="163" VerticalAlignment="Top" Width="380" Margin="10,59,0,0" Grid.Row="2" Grid.Column="1" Name="ProductGrid" Visibility="Collapsed">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="Hyperlink">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate x:Name="gridTemplate">
<StackPanel Orientation="Vertical" VerticalAlignment="Center" >
<HyperlinkButton Content="Hyperlinkbutton" Tag="Hyperlinkbutton" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
Code behind
private void ProductGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.DisplayIndex = 4;
templateColumn.CellTemplate = (DataTemplate)Resources["gridTemplate"];
ProductGrid.Columns.Add(templateColumn);
}
Instead of adding a new column, does setting the DisplayIndex of your existing column change its position?
<sdk:DataGridTemplateColumn x:Key="HyperlinkColumn" Header="Hyperlink">
and code
private void ProductGrid_AutoGeneratingColumn(object sender,
DataGridAutoGeneratingColumnEventArgs e)
{
var column = (DataGridColumn) Resources["HyperlinkColumn"];
column.DisplayIndex = 4;
}

MVVM WPF listbox mouse leftclick event firing

I am building a WPF application with MVVM architecture. In one form I have 2 listboxes and I want to perform filter based search. I am using a common search textbox, so I have to differentiate the search based on which listbox is selected. Please find my sample listbox below:
<HeaderedContentControl Header="Visible Objects:" Height="120" Width="250" Margin="20,20,20,0">
<ListBox Name="lstObjects" Height="100" Margin="5" ItemsSource="{Binding ProfileObjTypeToBind, Mode=OneWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="chkbxVisibleObjects" Grid.Column="1"
Content="{Binding Path=Value}" IsChecked="{Binding Path=flag,Mode=TwoWay}">
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</HeaderedContentControl>
<HeaderedContentControl Header="User Groups to View:" Height="120" Width="250" Margin="20,10,20,10">
<ListBox Name="lstGroups" Height="100" Margin="5" ItemsSource="{Binding ProfileUserGrpToBind, Mode=OneWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="chkAllowedGroups" Content="{Binding Path=GroupName}"
IsChecked="{Binding Path=flag,Mode=TwoWay}">
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</HeaderedContentControl>
All I want to do is identify the listbox selected and perform the filtering based on text entered in textbox. Please help me
Thanks a lot in advance.
You can't have a selected ListBox AND be able to write stuff to a TextBox. You can save the reference to your last ListBox though using SelectionChanged or some other method
private ListBox SelectedListBox = null;
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedListBox = (sender as ListBox);
}
once you have a reference to your last selected ListBox you can add TextChanged event to your TextBox:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (SelectedListBox == null)
return;
string searchText = (sender as TextBox).Text;
SelectedListBox.Items.Filter = (i) => { return ((string)i).Contains(searchText); }; // Or any other condition required
}

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