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
  1. Home
  2. General Programming
  3. WPF
  4. Editing items in a bound ListBox?

Editing items in a bound ListBox?

Scheduled Pinned Locked Moved WPF
wpfquestioncsharpwcfcom
18 Posts 3 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.
  • A ABitSmart

    I was writing my reply while you posted this. Well, yes, you can use one of the two, not both at a time. In your case, using ItemsSource renders Items readonly. But you can modify ItemsSource. The example I posted does that.

    D Offline
    D Offline
    David Veeneman
    wrote on last edited by
    #7

    Brilliant! Works like a champ. Thanks for your help on this. I cleaned up the code just a bit--complete code and XAML is posted below. XAML:

    <Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
    <TextBox Text="{Binding ElementName=playlists, Path=SelectedItem.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    <ListBox Name="playlists" ItemsSource="{Binding Path=Files}" SelectionMode="Single">
    <ListBox.ItemTemplate>
    <DataTemplate>
    <TextBlock Text="{Binding Name}"/>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>
    <Button Content="Show file list from DC" Click="Button_Click" />
    </StackPanel>
    </Window>

    Code:

    using System.Windows;
    using System.Collections.ObjectModel;
    using System.Text;

    namespace WpfApplication1
    {
    public partial class Window1 : Window
    {
    private PlayList _playlist = new PlayList();
    public Window1()
    {
    InitializeComponent();
    playlists.DataContext = _playlist;
    }

        private void Button\_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (MyFile file in \_playlist.Files)
            {
                sb.Append(file.Name + " ");
            }
            MessageBox.Show(sb.ToString());
        }
    }
    
    public class MyFile 
    { 
        public string Name { get; set; } 
    }
    
    public class PlayList 
    {
        public ObservableCollection<MyFile> Files { get; set; }
    
        public PlayList() 
        {
            Files = new ObservableCollection<MyFile>();
            Files.Add(new MyFile { Name = "a" });
            Files.Add(new MyFile { Name = "b" });
            Files.Add(new MyFile { Name = "c" });
            Files.Add(new MyFile { Name = "d" }); 
        } 
    }
    

    }

    David Veeneman www.veeneman.com

    A 1 Reply Last reply
    0
    • D David Veeneman

      Brilliant! Works like a champ. Thanks for your help on this. I cleaned up the code just a bit--complete code and XAML is posted below. XAML:

      <Window x:Class="WpfApplication1.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300">
      <StackPanel>
      <TextBox Text="{Binding ElementName=playlists, Path=SelectedItem.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
      <ListBox Name="playlists" ItemsSource="{Binding Path=Files}" SelectionMode="Single">
      <ListBox.ItemTemplate>
      <DataTemplate>
      <TextBlock Text="{Binding Name}"/>
      </DataTemplate>
      </ListBox.ItemTemplate>
      </ListBox>
      <Button Content="Show file list from DC" Click="Button_Click" />
      </StackPanel>
      </Window>

      Code:

      using System.Windows;
      using System.Collections.ObjectModel;
      using System.Text;

      namespace WpfApplication1
      {
      public partial class Window1 : Window
      {
      private PlayList _playlist = new PlayList();
      public Window1()
      {
      InitializeComponent();
      playlists.DataContext = _playlist;
      }

          private void Button\_Click(object sender, RoutedEventArgs e)
          {
              StringBuilder sb = new StringBuilder();
              foreach (MyFile file in \_playlist.Files)
              {
                  sb.Append(file.Name + " ");
              }
              MessageBox.Show(sb.ToString());
          }
      }
      
      public class MyFile 
      { 
          public string Name { get; set; } 
      }
      
      public class PlayList 
      {
          public ObservableCollection<MyFile> Files { get; set; }
      
          public PlayList() 
          {
              Files = new ObservableCollection<MyFile>();
              Files.Add(new MyFile { Name = "a" });
              Files.Add(new MyFile { Name = "b" });
              Files.Add(new MyFile { Name = "c" });
              Files.Add(new MyFile { Name = "d" }); 
          } 
      }
      

      }

      David Veeneman www.veeneman.com

      A Offline
      A Offline
      ABitSmart
      wrote on last edited by
      #8

      Glad to be of help :)

      U 2 Replies Last reply
      0
      • A ABitSmart

        Glad to be of help :)

        U Offline
        U Offline
        User 3671134
        wrote on last edited by
        #9

        Hi, I am facing the similar kind of issue. But I am actually changing the controTemplate of ListBoxItem itself with ContentPresenter & a TextBox. Did the same thing as per the posted contents but it did not work with the binded list. I have given something like below in the ControlTemplate of ListBox Item. ; Kindly suggest.

        A 1 Reply Last reply
        0
        • U User 3671134

          Hi, I am facing the similar kind of issue. But I am actually changing the controTemplate of ListBoxItem itself with ContentPresenter & a TextBox. Did the same thing as per the posted contents but it did not work with the binded list. I have given something like below in the ControlTemplate of ListBox Item. ; Kindly suggest.

          A Offline
          A Offline
          ABitSmart
          wrote on last edited by
          #10

          You code did not come through.

          U 2 Replies Last reply
          0
          • A ABitSmart

            Glad to be of help :)

            U Offline
            U Offline
            User 3671134
            wrote on last edited by
            #11

            Hi, I am facing the similar kind of issue. But I am actually changing the controTemplate of ListBoxItem itself with ContentPresenter & a TextBox. I did the same thing as per the posted contents but it did not work with the binded list. I have given something like below in the ControlTemplate of ListBox Item. <ContentPresenter x:Name="displayTextBlock" Grid.Column="0" Grid.ColumnSpan="1" Margin="11,5,11,5" HorizontalAlignment="Stretch"/> <TextBox x:Name="listItemTextBox" Padding="11,5,11,5" HorizontalAlignment="Stretch"                                                                                           Background="Transparent"                                                                                           Grid.Column="0"                                                                                           Visibility="Collapsed" Grid.ColumnSpan="1"                                                                                        Text="{Binding Path=Content, ElementName=displayTextBlock, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" /> Kindly suggest. Thnx, Ritesh

            1 Reply Last reply
            0
            • A ABitSmart

              You code did not come through.

              U Offline
              U Offline
              User 3671134
              wrote on last edited by
              #12

              See if it appears perfectly now.. Hi, I am facing the similar kind of issue. But I am actually changing the controTemplate of ListBoxItem itself with ContentPresenter & a TextBox. I did the same thing as per the posted contents but it did not work with the binded list. I have given something like below in the ControlTemplate of ListBox Item. <ContentPresenter x:Name="displayTextBlock" Grid.Column="0" Grid.ColumnSpan="1" Margin="11,5,11,5" HorizontalAlignment="Stretch"/> <TextBox x:Name="listItemTextBox" Padding="11,5,11,5" HorizontalAlignment="Stretch"                                                                                           Background="Transparent"                                                                                           Grid.Column="0"                                                                                           Visibility="Collapsed" Grid.ColumnSpan="1"                                                                                        Text="{Binding Path=Content, ElementName=displayTextBlock, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" /> Kindly suggest. Thnx, Ritesh

              A 1 Reply Last reply
              0
              • A ABitSmart

                You code did not come through.

                U Offline
                U Offline
                User 3671134
                wrote on last edited by
                #13

                Actually TextBox will be hidden & will be visible on DoubleClick. At that time, I want the content of ListBox item to be modified in Textbox & after lostfocus of TextBox, it should update ListBox Item & again becomes hidden. This works for unbound list but for bound list, I need your suggestions.

                A 1 Reply Last reply
                0
                • U User 3671134

                  See if it appears perfectly now.. Hi, I am facing the similar kind of issue. But I am actually changing the controTemplate of ListBoxItem itself with ContentPresenter & a TextBox. I did the same thing as per the posted contents but it did not work with the binded list. I have given something like below in the ControlTemplate of ListBox Item. <ContentPresenter x:Name="displayTextBlock" Grid.Column="0" Grid.ColumnSpan="1" Margin="11,5,11,5" HorizontalAlignment="Stretch"/> <TextBox x:Name="listItemTextBox" Padding="11,5,11,5" HorizontalAlignment="Stretch"                                                                                           Background="Transparent"                                                                                           Grid.Column="0"                                                                                           Visibility="Collapsed" Grid.ColumnSpan="1"                                                                                        Text="{Binding Path=Content, ElementName=displayTextBlock, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" /> Kindly suggest. Thnx, Ritesh

                  A Offline
                  A Offline
                  ABitSmart
                  wrote on last edited by
                  #14

                  It won't work because you are binding the TextBox to the ContentPresenter. There is no binding to your data element at all.

                  U 1 Reply Last reply
                  0
                  • A ABitSmart

                    It won't work because you are binding the TextBox to the ContentPresenter. There is no binding to your data element at all.

                    U Offline
                    U Offline
                    User 3671134
                    wrote on last edited by
                    #15

                    This is a custom control. In window, ItemSource of this control is bind to a collection Object. Do you suggest some other approach for this.

                    A 1 Reply Last reply
                    0
                    • U User 3671134

                      Actually TextBox will be hidden & will be visible on DoubleClick. At that time, I want the content of ListBox item to be modified in Textbox & after lostfocus of TextBox, it should update ListBox Item & again becomes hidden. This works for unbound list but for bound list, I need your suggestions.

                      A Offline
                      A Offline
                      ABitSmart
                      wrote on last edited by
                      #16

                      You should try something like this Link[^]

                      1 Reply Last reply
                      0
                      • U User 3671134

                        This is a custom control. In window, ItemSource of this control is bind to a collection Object. Do you suggest some other approach for this.

                        A Offline
                        A Offline
                        ABitSmart
                        wrote on last edited by
                        #17

                        But you have not bound any property of the collection to the ListBox item. How do you expect the collection to change?

                        U 1 Reply Last reply
                        0
                        • A ABitSmart

                          But you have not bound any property of the collection to the ListBox item. How do you expect the collection to change?

                          U Offline
                          U Offline
                          User 3671134
                          wrote on last edited by
                          #18

                          Hi, We are doing the binding as below: <eclp:EclpList   Name="eclpList" Margin="5" Height="Auto" Width="100" ItemsSource="{Binding}" DisplayMemberPath="Name" SelectionChanged="cmb_SelectionChanged"   /> See if this can give any ideas.. Thnx Ritesh

                          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