Editing items in a bound ListBox?
-
Post deleted because it's wrong. ABitSmart came up with the solution. David Veeneman Foresight Systems
David Veeneman www.veeneman.com
modified on Wednesday, March 25, 2009 11:22 AM
-
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.
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
-
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
-
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.
-
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.
-
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
-
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
-
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.
-
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
-
It won't work because you are binding the TextBox to the ContentPresenter. There is no binding to your data element at all.
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.
-
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.
-
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.
-
But you have not bound any property of the collection to the ListBox item. How do you expect the collection to change?
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