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
}
Related
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;
}
I am trying to detect which item in a listview is focused, but I am not getting the events detected. I am developing for Xbox One UWP, so I cannot use mouse or keyboard events, only focus can be used.
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" GotFocus="StackPanel_GotFocus" >
<StackPanel Name="Imagestack" Orientation="Horizontal">
<Image Source="{Binding Image}" Height="144" Width="256" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
private void StackPanel_GotFocus(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Image focus");
Image img = sender as Image;
Bgimage.Source = img.Source;
}
You should register to the ListView.GotFocus event.
The OriginalSource from the event parameter will be the ListViewItem which has received the focus. You can then retrieve the item content using ListViewItem.Content.
XAML:
<ListView x:Name="list" GotFocus="list_GotFocus">
<ListView.ItemTemplate>...</ListView.ItemTemplate>
</ListView>
Code behind:
private void list_GotFocus(object sender, RoutedEventArgs e)
{
var focusedItem = (e.OriginalSource as ListViewItem)?.Content;
}
You don't need to get focus state to get data from the clicked ListViewItem, the ItemClick event of the ListView may be what you're looking for:
<ListView x:Name="LV_Items"
IsItemClickEnabled="True"
ItemClick="LV_Items_ItemClick"
>
</ListView>
private void LV_Items_ItemClick(object sender, ItemClickEventArgs e)
{
// Get instance of the model in the clicked ListViewItem
MyModel myModel = (MyModel)e.ClickedItem;
Image img = myModel.Image;
}
I am having problem with listbox style
<ListBox Name="uiList" MaxHeight="300" Width="200">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel ForceCursor="False">
<CheckBox Margin="3" Focusable="False" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}">
<TextBlock Text="{Binding Title}"/>
</CheckBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
if (!SelectedItems.Contains((sender as CheckBox).DataContext))
{
SelectedItems.Add((sender as CheckBox).DataContext);
}
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
//If Unchecked CheckBox will remove from SelectedItems
SelectedItems.Remove((sender as CheckBox).DataContext);
}
Problem is when select item with mouse and keyboard they have a different style. First two one selected with keyboard others with mouse. Could someone help me with that.
Thank you!
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;
}
Here is the groupstyle of gridview in .xaml.
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Button x:Name="HeaderBtn" Click="HeaderButton_Click">
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="Title" Text="{Binding Key}" />
<TextBlock Text="{StaticResource ChevronGlyph}" />
</StackPanel>
</Button>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GourpStyle>
</GridView.GroupStyle>
The binding value key is the key of list.
Here is the button action.
private void HeaderButton_Click(object sender, RoutedEventArgs e)
{
}
When I click the button, how I get the text value of text block whose x:Name is "Title"?
2 options :
The MVVM way would be to use an ICommand instead of the event and pass the value as the command parameter.
<Button x:Name="HeaderBtn" Command="{Binding HeaderCommand}" CommandParameter="{Binding Title}">
In the event Handler, you should be able to retrieve the data context of the button, and the value of title
private void HeaderButton_Click(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
var dataContext = (HeaderClass)button.DataContext;
var title = dataContext.Title; //here is your value
}