none
WPF Listview Selecting ListViewItem Programmatically

    Question

  • Hi i have a wpf application with a textbox and a listview.
    I want to select items in the listview by typing in the textbox.

    This was easy using the older listview by using the FindItemByText function but the wpf listview doesnt contain such a function.

    I used this code to get the listviewitem selected but how do i get the listviewitem selected by using code.

    DependencyObject dep = (DependencyObject)e.OriginalSource;

    while ((dep != null) && !(dep is ListViewItem))

    {

    dep = VisualTreeHelper.GetParent(dep);

    } if (dep == null)

    return;

    Q1269.DataAccess.Matboard item = (Q1269.DataAccess.Matboard)listView1.ItemContainerGenerator.ItemFromContainer(dep); // Do something with the item...}



    this.textBoxMatCode.Text = item.Code.ToString();


    Thanx for any help!


    Ready for the Future
    http://www.qgo.co.za
    • Edited by WorsHound Friday, October 10, 2008 9:22 AM
    Sunday, July 06, 2008 4:50 PM

Answers

  • Well, this is what I do in my code (might not be the best), but I iterate through till I find it, then set it as selected: (I do not use LINQ, so I am assuming 'boards' is the collection and therefore 'board' is the item bound to each ListViewItem, I am also assuming that 'Code' is the bound property you are trying to select on and it is a string)

    for (int i = 0; i < listView1.Items.Count; i++)
    {
             if (((board)listView1.Items[i]).Code == "B")
             {
                    listView1.SelectedIndex = i;
                    break;
              }
    }

    It should work. just have to cast the item to the type you have, then use the property to match what you want.

    Hope this helps!

     

    Noorbakhsh

    • Edited by noorbakhsh Monday, July 07, 2008 5:01 PM Typo
    • Marked as answer by WorsHound Wednesday, July 09, 2008 6:58 AM
    Monday, July 07, 2008 4:59 PM

All replies

  • Hi,

    I don't really understand your requirement. Since the ListViewItem has Content property, it can hold any type of daya. So how can we search in Listof Objects using String (Referring the Text property of the TextBox because a TextBox can only hold Text).

    Further, We can programatically select any item using SelectedItem or SelectedValue property. The Below sample works absolutely fine:

        Private Sub Window1_Loaded(ByVal sender As ObjectByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded  
            Dim procList As Process() = Process.GetProcesses()  
            listview1.ItemsSource = From p In procList Select p.ProcessName  
            listview1.SelectedItem = "System" 
        End Sub 

    Regards,
    Harish
    Wisdom is an art to know, what to ignore.
    Sunday, July 06, 2008 6:27 PM
  • If i understood you correctly to get the listview selected item you need to use selectionchanged event of the listview


    <TextBox Height="38" Width="133" Canvas.Left="40" Canvas.Top="157" x:Name="textbox1"/>

    <ListView Grid.Row="1" Margin="28,38,72,135" Name="listview1" SelectedIndex="0" Width="199" Height="77" SelectionChanged="listview1_SelectionChanged">

    <ListViewItem Content="1"/>

    <ListViewItem Content="2"/>

    <ListViewItem Content="3"/>

    <ListViewItem Content="4"/>

    <ListViewItem Content="5"/>

    </ListView>

    and in the selectionchanged handler you can do your logic

    private void listview1_SelectionChanged(object sender, SelectionChangedEventArgs e)

    {

    ListViewItem lstSelected = (ListViewItem)e.AddedItems[0];

    if (lstSelected != null)

    {

    textbox1.Text = lstSelected.Content.ToString();

    }

    }

    Hope this helps


    Alaguraja
    Monday, July 07, 2008 8:51 AM
  •  I think many people will use ListView with bound data.So the bound data's type is not ListViewItem.
    we should do the following:

    listView.ItemContainerGenerator.ContainerFromItem(obj item) 
    Monday, July 07, 2008 8:59 AM
  •  

    Hi

    Thank you for your replys.

     

    I have a wpf listview that uses a datatemplate to display data in the view:

     

    <DataTemplate>

    <StackPanel Width="70"  VerticalAlignment="Top">

    <Image Name="ImageSample" Source="{Binding Path=Code}..}"/>

    <TextBlock Name="TextCode" Text="{Binding Path=Code}" />

    <TextBlock Name="TextColour " Text="{Binding Path=Colour}" />

    </StackPanel>

    </DataTemplate>

     

    The data is populated using a datacontext:

     

    Q1269.DataAccess.Q1269DataContext db = new Q1269.DataAccess.Q1269DataContext("Q1269.sdf");

     

    var boards = from Boards in db. boards

                 select Boards;

     

    this.listView1.DataContext = boards;

     

    I need to select a spesific item in the listview when I click on a button.

    When I use the listview.selectedvalue or selectedindex a null value is returned I need a way to select a item in my list how can I acomplich this.

     

    Lets say my list contains A B and C how do I get B to be selected by clicking a button.

     

    Thank you.


    Ready for the Future
    http://www.qgo.co.za
    • Edited by WorsHound Friday, October 10, 2008 9:23 AM
    Monday, July 07, 2008 12:37 PM
  • I have a similar situation to yours. I have a graph that displays my data and as well as a list view that displays the same data in more detail.  So when I click on a data point in my graph I have it selected in the listview and vice versa.

    To accomplish this I have a separate observablecollection that contains my currently selected items.  Now my usercontrol that contains the listview has a bunch of dependencies one of which is this observable collection.  So for that dependency i have a propertyChangedCallback that gets triggered whenever I add/remove items from that collection.  In there i traverse my listview.selecteditems list and remove any items that are there but not in my selected collection (this takes care of items that are clicked again thus removing them) then i traverse my selected collection and force the listview to select it

    listView.SelectedItems.Add(obj) 

    this works for me as bothe the itemssource on the list is the same type of data as in the selected collection, the list view gets templated later as you do but that does not matter for me as im comparing actual source data rather than the templated data.

    Hope this helps



    Monday, July 07, 2008 1:46 PM
  • Well, this is what I do in my code (might not be the best), but I iterate through till I find it, then set it as selected: (I do not use LINQ, so I am assuming 'boards' is the collection and therefore 'board' is the item bound to each ListViewItem, I am also assuming that 'Code' is the bound property you are trying to select on and it is a string)

    for (int i = 0; i < listView1.Items.Count; i++)
    {
             if (((board)listView1.Items[i]).Code == "B")
             {
                    listView1.SelectedIndex = i;
                    break;
              }
    }

    It should work. just have to cast the item to the type you have, then use the property to match what you want.

    Hope this helps!

     

    Noorbakhsh

    • Edited by noorbakhsh Monday, July 07, 2008 5:01 PM Typo
    • Marked as answer by WorsHound Wednesday, July 09, 2008 6:58 AM
    Monday, July 07, 2008 4:59 PM