Click here to Skip to main content
15,887,027 members
Home / Discussions / WPF
   

WPF

 
QuestionTrackball.cs problem Pin
grJubei25-Mar-09 5:57
grJubei25-Mar-09 5:57 
QuestionMusic Player in WPF.. Pin
VisualLive25-Mar-09 0:06
VisualLive25-Mar-09 0:06 
AnswerRe: Music Player in WPF.. Pin
BlitzPackage27-Mar-09 15:37
BlitzPackage27-Mar-09 15:37 
QuestionTreeview Pin
Ch.Gayatri Subudhi24-Mar-09 18:04
Ch.Gayatri Subudhi24-Mar-09 18:04 
AnswerRe: Treeview Pin
Mark Salsbery25-Mar-09 6:56
Mark Salsbery25-Mar-09 6:56 
QuestionEditing items in a bound ListBox? Pin
David Veeneman24-Mar-09 12:25
David Veeneman24-Mar-09 12:25 
AnswerRe: Editing items in a bound ListBox? Pin
ABitSmart24-Mar-09 19:00
ABitSmart24-Mar-09 19:00 
GeneralRe: Editing items in a bound ListBox? Pin
David Veeneman25-Mar-09 3:04
David Veeneman25-Mar-09 3:04 
Thanks, but I pasted your code and markup into a WPF project, and I still have the same problem. I have pasted my complete code and markup below.

What I want to do is implement two-way binding between the listbox and the textbox; i.e., click on a listbox item and have it appear in the textbox. Then, edit the item in the textbox and have the changes propogated back to the list box. If the listbox ItemsSource property is bound to a DataContext, two-way databinding between the listbox and the textbox doesn't work--it's only one way, from the listbox to the textbox.

To see what I mean, remove the ItemsSource binding from the listbox, then add the four PlayList strings to the listbox using the Designer's Items property. Change the textbox binding path to SelectedValue.Content, and run. You will have two-way binding between the textbox and the listbox. Edit the textbox, and the changes appear in the listbox.

Now, restore the original code. The textbox-listbox binding is now only one-way--from the listbox to the textbox. Changes in the textbox are not propagated back to the listbox.

That brings me back to my original question: Is there any way to implement two-way binding between the textbox and the listbox, while maintaining the listbox ItemsSource binding to the PlayList object?

My markup:

<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=SelectedValue.Content, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <ListBox Name="playlists"  SelectionMode="Single">
            <ListBoxItem>a</ListBoxItem>
            <ListBoxItem>b</ListBoxItem>
            <ListBoxItem>c</ListBoxItem>
            <ListBoxItem>d</ListBoxItem>
        </ListBox>
        <!-- ItemsSource="{Binding Path=Files}" -->
    </StackPanel>
</Window>



My code:

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

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

    public class PlayList 
    { 
        public ObservableCollection<string> Files { get; set; } 
        public PlayList() 
        {
            Files = new ObservableCollection<string>(); 
            Files.Add("a"); 
            Files.Add("b"); 
            Files.Add("c"); 
            Files.Add("d"); 
        } 
    }
}


David Veeneman
www.veeneman.com

GeneralRe: Editing items in a bound ListBox? Pin
ABitSmart25-Mar-09 3:53
ABitSmart25-Mar-09 3:53 
AnswerRe: Editing items in a bound ListBox? [modified] Pin
David Veeneman25-Mar-09 3:50
David Veeneman25-Mar-09 3:50 
GeneralRe: Editing items in a bound ListBox? Pin
ABitSmart25-Mar-09 4:02
ABitSmart25-Mar-09 4:02 
GeneralRe: Editing items in a bound ListBox? Pin
David Veeneman25-Mar-09 5:29
David Veeneman25-Mar-09 5:29 
GeneralRe: Editing items in a bound ListBox? Pin
ABitSmart25-Mar-09 5:40
ABitSmart25-Mar-09 5:40 
GeneralRe: Editing items in a bound ListBox? Pin
Member 367392230-Jun-09 20:50
Member 367392230-Jun-09 20:50 
GeneralRe: Editing items in a bound ListBox? Pin
ABitSmart30-Jun-09 20:56
ABitSmart30-Jun-09 20:56 
GeneralRe: Editing items in a bound ListBox? Pin
Member 367392230-Jun-09 21:00
Member 367392230-Jun-09 21:00 
GeneralRe: Editing items in a bound ListBox? Pin
ABitSmart30-Jun-09 21:21
ABitSmart30-Jun-09 21:21 
GeneralRe: Editing items in a bound ListBox? Pin
Member 367392230-Jun-09 21:34
Member 367392230-Jun-09 21:34 
GeneralRe: Editing items in a bound ListBox? Pin
ABitSmart30-Jun-09 21:37
ABitSmart30-Jun-09 21:37 
GeneralRe: Editing items in a bound ListBox? Pin
Member 367392230-Jun-09 22:45
Member 367392230-Jun-09 22:45 
GeneralRe: Editing items in a bound ListBox? Pin
Member 367392230-Jun-09 21:05
Member 367392230-Jun-09 21:05 
GeneralRe: Editing items in a bound ListBox? Pin
ABitSmart30-Jun-09 21:35
ABitSmart30-Jun-09 21:35 
GeneralRe: Editing items in a bound ListBox? Pin
Member 367392230-Jun-09 20:56
Member 367392230-Jun-09 20:56 
QuestionDatabinding textbox to double Pin
ErikZ24-Mar-09 2:46
ErikZ24-Mar-09 2:46 
AnswerRe: Databinding textbox to double Pin
Pete O'Hanlon24-Mar-09 3:56
mvePete O'Hanlon24-Mar-09 3:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.