CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2016
    Posts
    1

    WPF Data Binding between a tab control and a tree view

    I am getting back to learning WPF and am writing a very simple application. In this application a tab control displays note tabs for the selected item in a tree view control, using a binding to a property of that items which contains an observable collection. Unfortunately, when I select an item, even when the item has objects in its property that the tab control is bound to, no tabs appear.

    I have a tree view control name nodeTreeView.
    I have a tab control name noteTabs.
    The tree view controls has an items source of ObservableCollection<Node>.
    The tab control has an items source of {Binding Source=nodeTreeView, Path=SelectedItem.Notes}
    Notes is a property of Node containing an observable collection<Note>.

    I have gotten the tab control to display items before when I used a specific items source. But now that I have used a binding to point to a collection in a property of the selected item of the tree view control, it doesn't work.

    Does anyone have any idea?

    I will post the code below but please understand that since I simply slapped it together for the purpose of learning that it is very sloppy.

    Code:
    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApplication1"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto">
                </RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
            <Menu Grid.Row="0">
                <MenuItem Header="File">
                    <MenuItem Header="New"></MenuItem>
                    <Separator/>
                    <MenuItem Header="Save"></MenuItem>
                    <MenuItem Header="Save As"></MenuItem>
                    <Separator/>
                    <MenuItem Header="Exit"></MenuItem>
                </MenuItem>
            </Menu>
            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TreeView x:Name="nodeTreeView" Grid.Column="0" HorizontalAlignment="Stretch">
                    <TreeView.ItemTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
                            <TextBlock Text="{Binding Path=Title}"></TextBlock>
                        </HierarchicalDataTemplate>
                    </TreeView.ItemTemplate>
                </TreeView>
                <GridSplitter Width="5" HorizontalAlignment="Stretch" Grid.Column="1"></GridSplitter>
                <TabControl x:Name="noteTabs"
                            HorizontalAlignment="Stretch" 
                            VerticalAlignment="Stretch" Grid.Column="2"
                            ItemsSource="{Binding Source=nodeTreeView, Path=SelectedItem.Notes}">
                    <TabControl.ContentTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Text, Mode=TwoWay}"></TextBox>
                        </DataTemplate>
                    </TabControl.ContentTemplate>
                    <TabControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Title}"></TextBlock>
                        </DataTemplate>
                    </TabControl.ItemTemplate>
                </TabControl>
            </Grid>
            <StatusBar Grid.Row="2">
                <StatusBarItem Content="Yo"></StatusBarItem>
            </StatusBar>
        </Grid>
    </Window>
    Code:
    using System;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    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;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                ObservableCollection<Node> nodes = new ObservableCollection<Node>();
                nodes.Add(new Node("Root"));
                nodes[0].Children.Add(new Node("Node 1"));
                nodes[0].Children.Add(new Node("Node 2"));
    
                nodeTreeView.ItemsSource = nodes;
                
                var notes = nodes[0].Children[0].Notes;
                notes.Add(new Note("Note 1", "This is the first note."));
                notes.Add(new Note("Note 2", "This is the second note."));
    
                
            }
        }
    
        public class Node : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            public ObservableCollection<Node> Children { get; }
            public ObservableCollection<Note> Notes { get; }
    
            public Node(string title)
            {
                Title = title;
                Children = new ObservableCollection<Node>();
                Notes = new ObservableCollection<Note>();
            }
    
            private string _title;
    
            public string Title
            {
                get
                {
                    return _title;
                }
    
                set
                {
                    if (_title != value)
                    {
                        _title = value;
                        NotifyPropertyChanged(nameof(Title));
                    }
                }
            }
    
            private void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    
        public class Note : INotifyPropertyChanged
        {
            private string _title;
            private string _text;
    
            public string Title
            {
                get
                {
                    return _title;
                }
    
                set
                {
                    if (_title != value)
                    {
                        _title = value;
                        NotifyPropertyChanged(nameof(Title));
                    }
                }
            }
    
            public string Text
            {
                get
                {
                    return _text;
                }
    
                set
                {
                    if (_text != value)
                    {
                        _text = value;
                        NotifyPropertyChanged(nameof(Text));
                    }
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
    
            public Note(string title, string text)
            {
                Title = title;
                Text = text;
            }
    
            private void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

  2. #2
    Join Date
    May 2002
    Posts
    511

    Re: WPF Data Binding between a tab control and a tree view

    What is the value for SelectedItem.Notes?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured