Murali
Murali

Reputation: 41

Listbox item selection programmatically

I have an hamburger Menu with ListBox, When I click on items it is Highlighted and navigated to related page, but when I click on Any link inside the xaml page the hamburger item is not changing even the page is navigated. How to set Listbox item selected and highlighted from code behind.

I am calling the following function from wherever I need to change the Listbox item to my required one

public async static void ChangeSideBarItem()
{
    ADHome objADHome = new ADHome();
    string selected = App.SelectedPage;

    int x = objADHome.SideBarListBox.Items.IndexOf(selected);
    if (!String.IsNullOrEmpty(selected))
    {
        for (int index = 0; index < objADHome.SideBarListBox.Items.Count; index++)
        {
            HamburgerItemClass item = (HamburgerItemClass)objADHome.SideBarListBox.Items[index];

            string item1 = item.Title;
            if (selected == item1)
            {
                objADHome.SideBarListBox.SelectedItem = index;
                break;
            }
        }
    }
}

I am calling the above method by setting the App.SelectedPage from different xaml page(orders Page) here i have a link Dashboard to navigate back to dashboard page, when i click on that the particular dashboard item in listbox(Sidebar) should be highlighted

private void DashboardLink_Click(object sender, RoutedEventArgs e)
{
    App.SelectedPage = "Dashboard";
    ADHome.ChangeSideBarItem();
    this.Frame.Navigate(typeof(DashBoard));
}

But its not selected, when I debug it, shows null in selecteditem

Upvotes: 2

Views: 568

Answers (3)

Jayden
Jayden

Reputation: 3286

If we set the Collection to the ItemSource of the ListBox, we should be able to use the to set to the ListBox.SelectedItem. When my Collection is ObservableCollection<Menu> Menus, we should be able to use the Menu object set to the ListBox.SelectedItem.

For example:

SideBarListBox.SelectedItem = Menus[1]; 

If we set the ListBoxItem by the following code, we should be able to use the ListBoxItem set to the ListBox.SelectedItem.

<ListBox Name="MyListBox">
    <ListBox.Items>
        <ListBoxItem Name="FristItem">
            <StackPanel Orientation="Horizontal">
                <TextBlock  FontFamily="Segoe MDL2 Assets" FontSize="36" Text="&#xE170;"></TextBlock>
            </StackPanel>
        </ListBoxItem>
        <ListBoxItem Name="SecondItem">
            <StackPanel Orientation="Horizontal">
                <TextBlock  FontFamily="Segoe MDL2 Assets" FontSize="36" Text="&#xE171;"></TextBlock>
            </StackPanel>
        </ListBoxItem>
    </ListBox.Items>
</ListBox>

By the way, We should be able to use the ListBox.SelectedIndex to set the selected item.

When we set that the SelectedItem will be highlighted, if you want to change the other color to it, we should be able to use the ListBox.ContainerFromItem to get the ListBoxItem. Then we can set the Background and the Foreground.

Upvotes: 0

Anastasios Moraitis
Anastasios Moraitis

Reputation: 388

I think that you can override the onNavigatedTo() method and change the color of the item to the default. In the (Frame).Navigate() pass as argument to the other frame the .SelectedItem of the list to make the comparison. Sorry for my English.

Upvotes: 0

Romasz
Romasz

Reputation: 29792

Generally you can just add your items to ListBox.SelectedItems collection:

private void SelectButton_Click(object sender, RoutedEventArgs e)
{
    // either by adding item from collection:
    myListBox.SelectedItems.Add(CollectionOfItems[1]);

    // or add from list itself:
    myListBox.SelectedItems.Add(myListBox.Items.Last());
}

Upvotes: 1

Related Questions