Treeview selected item changes to root item on service call

332 Views Asked by At

I have a TreeView in a window that is being opened as a dialog. When an item is clicked on it gets selected and. But when there is a service call the selected item changes to the root item. The DataContext for the TreeView is an array of "authors", each containing an array of "books", which contain an array of "pages". I get the array of "authors" from a service call as well as the bytes for the "page" PDF. The "authors" and "books" are shown in the TreeView but the "pages" are not. When a "book" is clicked the array of "pages" is loaded into the combobox and the first "page" is displayed in a PDF viewer. The "page" is loaded from the server by a service call. The root "author" item in the TreeView is selected only if I use the service call.

NOTE: This is the code that I am actually using but names of things are changed because of the privacy policy of my company. Everything functionally and structurally is the same though.

Here is essentially what the XAML and code behind is.

XAML:

<Window.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:author}" ItemsSource="{Binding Path=books}">
            <TextBlock Text="{Binding name}"/>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:book}" >
            <TextBlock Text="{Binding title}"/>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" MinWidth="125"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <GridSplitter HorizontalAlignment="Right" VerticalAlignment="Stretch" Grid.Column="1" Grid.RowSpan="2" ResizeBehavior="PreviousAndNext" Width="5" Background="#FFBCBCBC"/>

        <TreeView x:Name="booksTreeView" Grid.Column="0" Grid.RowSpan="2" SelectedItemChanged="booksTreeView_SelectedItemChanged" Background="WhiteSmoke"/>

        <ComboBox x:Name="pagesDropBox" Grid.Column="2" Grid.Row="0" HorizontalAlignment="Left" MinWidth="100" SelectionChanged="pagesDropBox_SelectionChanged" />

        <Grid Grid.Column="2" Grid.Row="1" x:Name="previewHolderGrid"/>
    </Grid>

C#:

private void booksTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tree = sender as TreeView;
            book b = tree.SelectedItem as book;
            if (b != null)
            {
                e.Handled = true;
                pagesDropBox.ItemsSource = db.pages;
                if (b.pages.Count > 0)
                    pagesDropBox.SelectedIndex = 0;
            }
        }

        private void pagesDropBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            previewHolderGrid.Children.Clear();
            Guid p = pagesDropBox.SelectedItem as Guid;
            if (p != Guid.Empty)
            {
                byte[] bytes = ServiceAccess.Instance.GetPage(p); //This is the line that will cause the root item to be selected even if the next line is commented out
                this.previewHolderGrid.Children.Add(new PDFViewer.PdfViewerControl(bytes));
            }
        }

Datacontext objects:

public class author{
    String name;
    BindingList<book> books;

}

public class book{
    String title;
    BindingList<Guid> pages;
}
0

There are 0 best solutions below