views:

48

answers:

1

Hello dear programers,

how u doin? I have a problem. I want to bind multiple objects to a single textbox for example. Lets say I have a list with many tasks. Each task contains a title.

public class Task
{
public string Title { get; set; }
[...]
}

Now I want to select two tasks in a listbox. If the title of both tasks are the same I want the textbox to display the title. If they are different it should display nothing.

If the user changes the value the title of both tasks should be changed to the new value.

I created a new property 'Title' so far which is binded in XAML. There are 2 problems.

  1. If I set the DataContext of the grid the program reads the 'Title' only once. If set the datacontext to null and then to the Task class again it works (ugly though).

  2. If I change the title it wont be changed instantly in the listbox. Only if the listbox reads the list of tasks again it will be displayed properly.

    public string Title
    {
        get
        {
            string title = Tasks[0].Title;
    
    
    
        for (int i = 1; i < Tasks.Count; i++)
        {
            if (title != Tasks[i].Title)
                return "";
        }
    
    
        return title;
    }
    set
    {
        foreach (Task task in Tasks)
            task.Title = value;
    }
    
    }

Thank you for your help

Greetings stfx

A: 

Use a MultiValueConverter. Its' purpose is to implement converters that support multiple bindings (MultiBinding objects), so you can bind to more than one task.

luvieere