Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WCF and WF
  4. Databinding for editable tree view

Databinding for editable tree view

Scheduled Pinned Locked Moved WCF and WF
wpfwcfdata-structureshelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    califax2k
    wrote on last edited by
    #1

    Hi, my problem is the following: I am binding a data structure to a tree view and I want to edit elements of this data structure. But when a TreeViewItem has been selected the TextBox doesn't contain the Name (that's the part of the data element that is to be changed) but its empty. The data elements look like this:

    public class PiList
        : INotifyPropertyChanged
    {
    
        private string m\_name;
        // This is the one that is to be changed.
        public string Name
        {
            get
            {
                return m\_name;
            }
            set
            {
                m\_name = value;
                OnPropertyChanged("Name");
            }
        }
    
        private readonly ObservableCollection \_children = new ObservableCollection();
        public ObservableCollection Children
        {
            get
            {
                return \_children;
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
    

    For this data structure I have the following data template:

    <HierarchicalDataTemplate DataType="{x:Type local:PiList}" ItemsSource="{Binding Path=Children}">
    <ContentControl Content="{Binding Name}" Style="{StaticResource EditableContentControl}"/>
    </HierarchicalDataTemplate>

    This template applies the style EditableContentControl to the ContentControl. This style is defined like this:

    <Style x:Key="EditableContentControl" TargetType="{x:Type ContentControl}">

    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate DataType="{x:Type local:PiList}">
                <TextBlock Text="{Binding Path=Name}" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
    
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsSelected,RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
                     Value="True">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate DataType="{x:Type local:PiLis
    
    C M 2 Replies Last reply
    0
    • C califax2k

      Hi, my problem is the following: I am binding a data structure to a tree view and I want to edit elements of this data structure. But when a TreeViewItem has been selected the TextBox doesn't contain the Name (that's the part of the data element that is to be changed) but its empty. The data elements look like this:

      public class PiList
          : INotifyPropertyChanged
      {
      
          private string m\_name;
          // This is the one that is to be changed.
          public string Name
          {
              get
              {
                  return m\_name;
              }
              set
              {
                  m\_name = value;
                  OnPropertyChanged("Name");
              }
          }
      
          private readonly ObservableCollection \_children = new ObservableCollection();
          public ObservableCollection Children
          {
              get
              {
                  return \_children;
              }
          }
      
          public event PropertyChangedEventHandler PropertyChanged;
          void OnPropertyChanged(string name)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(name));
              }
          }
      }
      

      For this data structure I have the following data template:

      <HierarchicalDataTemplate DataType="{x:Type local:PiList}" ItemsSource="{Binding Path=Children}">
      <ContentControl Content="{Binding Name}" Style="{StaticResource EditableContentControl}"/>
      </HierarchicalDataTemplate>

      This template applies the style EditableContentControl to the ContentControl. This style is defined like this:

      <Style x:Key="EditableContentControl" TargetType="{x:Type ContentControl}">

      <Setter Property="ContentTemplate">
          <Setter.Value>
              <DataTemplate DataType="{x:Type local:PiList}">
                  <TextBlock Text="{Binding Path=Name}" />
              </DataTemplate>
          </Setter.Value>
      </Setter>
      
      <Style.Triggers>
          <DataTrigger Binding="{Binding Path=IsSelected,RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
                       Value="True">
              <Setter Property="ContentTemplate">
                  <Setter.Value>
                      <DataTemplate DataType="{x:Type local:PiLis
      
      C Offline
      C Offline
      califax2k
      wrote on last edited by
      #2

      Further investigation: also setting the data context of the treeview to make the bindings refer implicitly to this data context didn't help :confused:

      #pragma error( disable : * )

      1 Reply Last reply
      0
      • C califax2k

        Hi, my problem is the following: I am binding a data structure to a tree view and I want to edit elements of this data structure. But when a TreeViewItem has been selected the TextBox doesn't contain the Name (that's the part of the data element that is to be changed) but its empty. The data elements look like this:

        public class PiList
            : INotifyPropertyChanged
        {
        
            private string m\_name;
            // This is the one that is to be changed.
            public string Name
            {
                get
                {
                    return m\_name;
                }
                set
                {
                    m\_name = value;
                    OnPropertyChanged("Name");
                }
            }
        
            private readonly ObservableCollection \_children = new ObservableCollection();
            public ObservableCollection Children
            {
                get
                {
                    return \_children;
                }
            }
        
            public event PropertyChangedEventHandler PropertyChanged;
            void OnPropertyChanged(string name)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(name));
                }
            }
        }
        

        For this data structure I have the following data template:

        <HierarchicalDataTemplate DataType="{x:Type local:PiList}" ItemsSource="{Binding Path=Children}">
        <ContentControl Content="{Binding Name}" Style="{StaticResource EditableContentControl}"/>
        </HierarchicalDataTemplate>

        This template applies the style EditableContentControl to the ContentControl. This style is defined like this:

        <Style x:Key="EditableContentControl" TargetType="{x:Type ContentControl}">

        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate DataType="{x:Type local:PiList}">
                    <TextBlock Text="{Binding Path=Name}" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
        
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsSelected,RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
                         Value="True">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate DataType="{x:Type local:PiLis
        
        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        Try changing your hierarchical data template to

        <HierarchicalDataTemplate DataType="{x:Type local:PiList}" ItemsSource="{Binding Path=Children}">
            <ContentControl `Content="{Binding}"` Style="{StaticResource EditableContentControl}" />
        </HierarchicalDataTemplate>
        

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        C 1 Reply Last reply
        0
        • M Mark Salsbery

          Try changing your hierarchical data template to

          <HierarchicalDataTemplate DataType="{x:Type local:PiList}" ItemsSource="{Binding Path=Children}">
              <ContentControl `Content="{Binding}"` Style="{StaticResource EditableContentControl}" />
          </HierarchicalDataTemplate>
          

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          C Offline
          C Offline
          califax2k
          wrote on last edited by
          #4

          You're so cool, Thanks a lot ;-)

          #pragma error( disable : * )

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups