Controlling the initially selected item?

Jun 5, 2012 at 9:42 PM
Edited Jun 5, 2012 at 10:56 PM

I am setting the itemsource of the NavigationPane control to a collection of NavigationPaneItems. I am trying to persist the selected item when a user closes and reopens the application. So as I build the collection of NavigationPaneItems I set the IsSelected to true on the item that was selected last time the application was used. The problem that I am running into is that it seems like the control automatically sets the selectedindex of the NavigationPane to zero when the itemsource is set. The result is that both the first item and the item that I was trying to select are selected.

I have tried looking through the source code to see where this is happening but have so far been unsuccessful. Does anyone know where this selection of the first item is happening in the source of the control or how I can get past this issue?

I have created a simple test application to recreate the issue. In the test application I am building 10 items and want the 3rd item selected on startup.

Here is the xaml:

 

<Window x:Class="OutlookNavigationPanelTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="http://www.stemasoft.com/xaml"
        xmlns:my="clr-namespace:OutlookNavigationPanelTest"
        Title="MainWindow" Height="600" Width="300">

    <Grid>
        <s:NavigationPane x:Name="navigationPane" />
    </Grid>
</Window>
 

 

And here is the code behind:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Stema.Controls;
using System.Collections.ObjectModel;

namespace OutlookNavigationPanelTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            navigationPane.ItemsSource = GenerateTestNavItems();
        }

        private ObservableCollection<NavigationPaneItem> GenerateTestNavItems()
        {
            ObservableCollection<NavigationPaneItem> result = new ObservableCollection<NavigationPaneItem>();

            int counter = 0;

            for (int i = 1; i <= 10; i++)
            {
                NavigationPaneItem item = new NavigationPaneItem();
                item.Content = i.ToString();
                item.Header = i.ToString();

                if (counter == 2)
                    item.IsSelected = true;
                else
                    item.IsSelected = false;

                item.Image = new ImageSourceConverter().ConvertFromString("pack://application:,,,/OutlookNavigationPanelTest;component/Images/sun_32.png") as ImageSource;
                item.ImageSmall = item.Image;
                result.Add(item);
                counter++;
            }

            return result;
        }
    }
}

An additional problem that I am experiencing is that selecting an item from the overflow list does not fire the SelectionChanged Event. Is there another event that I should be using for overflow items?

Jun 7, 2012 at 1:00 AM
Edited Jun 7, 2012 at 1:02 AM

There are tow properties u can use to control selection. First one is SelectedIndex which takes integer value and Another one is SelectedItem. For using index just provide value with index of element u set as selected when u close your app. If u go with SelectedItem, u can either create dependency property in your code-behind file and bind SelectedItems' value to it or, if u are MVVM follower u could create property in your ViewModel class and inside its Getter use LINQ to select first item which has IsSelected to true inside your collection.

public NavigationPaneItem SelectedItem
{
     get{ return items.FirstOrDefault(i=>i.IsSelected == True);}
}