3

Let's say I have a ListBox which binds to stuff in code-behind:

<ListBox x:Name="list"> <ListBox.ItemTemplate> <DataTemplate> <ListBoxItem Content="{Binding Name}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <TextBox x:Name="name" Text="{Binding ElementName=list, Path=SelectedItem.Name, Mode=TwoWay" /> <TextBox x:Name="contents" Text="{Binding ElementName=list, Path=SelectedItem.Contents, Mode=TwoWay" /> 

Code behind:

public class Dude { public String Name { get; set; } public String Contents { get; set; } } 

Now the above does just what I want it to. When an item in the listbox is selected, The textboxes update to show what was selected in the listbox.

But what I am now trying to do is expand on my Dude class by adding a Dictionary to it:

public class Dude { public string Name { get; set; } public string Contents { get; set; } public Dictionary<String, String> Tasks { get; set; } } 

In the hopes that I can:

Click an Item in the ListBox, have the corresponding item's Name and Contents properties display in their respective TextBoxes and then Append to the contents textbox the Key/Value of the Dictionary's contents.

But I don't know how I can go that deep. It kinda feels like I'm going multiple levels, would something like Multidimensional binding be what I need?

Are there any (simple) samples you have, or have seen? Docs, articles, tutorials?

Any help is much appreciated.

Thank you

3
  • 2
    I think the better solution will probably involve a list box or other ItemsControl that you bind to the Tasks property, not appending the Tasks pairs to the control that you've bound to the Contents property. But it's been a long time since I have done XAML binding, so I don't have a better answer for you now, I'm afraid. Good luck! Commented Sep 29, 2012 at 22:55
  • 1
    This may be of use drwpf.com/blog/2007/09/16/… Commented Sep 29, 2012 at 23:29
  • 1
    your dictionary has to be static or i don't see the use. what you could do is to change the binding to make it notify on target updated, then catch the TargetUpdated event. (msdn.microsoft.com/en-us/library/…) Commented Sep 29, 2012 at 23:40

1 Answer 1

3

What you want can be done in WPF (bit harder in other XAML technologies like Wp7 or WinRT), but I'm not sure it's what you need...

Use MultiBinding, to bind both the Contents string and the Tasks dictionary to the second textbox, then write your own IMultiValueConverter to build the string you want to display.

Read the tutorial about MultiBindings here, and the rough code should look something like this:

<TextBox> <TextBox.Text> <MultiBinding Converter="{StaticResource YourAppendingConverter}"> <Binding ElementName="list" Path="SelectedItem.Contents" /> <Binding ElementName="list" Path="SelectedItem.Tasks" /> </MultiBinding> </TextBox.Text> </TextBox> 

and your converter should resemble:

public class YourAppendingConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture){ StringBuilder sb = new StringBuilder(values[0].ToString()); sb.AppendLine("Tasks:"); foreach (var task in (Dictionary<string,string>)values[1]){ sb.AppendLine(string.Format("{0}: {1}", task.Key, task.Value)); } return sb.ToString(); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture){ throw new NotSupportedException(); } 

Why do I think this is what you need?

  1. Writing a ConvertBack if needed is hell - so this TextBox should be readonly
  2. Dictionary is not notifying. If the underlying dictionary changes while you displaying it, nothing will change

If you think these through, then MultiBinding might be the tool you need.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thank you very much @TDaver. It's times like these where I really miss the simplicity of WinForms.
@ProtectedIdentity: I actually find this much more logical :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.