views:

20

answers:

1

I've spent hours on this so any help is much appreciated:

I have a TextBlock inside a DataTemplate which has an ItemsSource already set. I want to bind the Text of the TextBlock and set the Path to whatever the path is of a different object in code-behind. I've tried everything I can think of even this:

<TextBlock Text="{Binding Path='{Binding ElementName=sendingComboColumn,Path=DisplayMemberPath}'}" />

I just can't figure out how to dynamically set the path!! It has to be generic depending on which column called it.

A: 

I finally found a solution! Rather than trying to dynamically change the path inside the DataTemplate I just create a new DataTemplate in code and assign the ListBox.ItemTemplate to the new DataTemplate:

public static DataTemplate CreateTemplate(string path)
        {
            return (DataTemplate)XamlReader.Load(
                @"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007""&gt;
                    <StackPanel Orientation='Horizontal'>
                        <CheckBox IsChecked='{Binding}' />
                        <TextBlock Text='{Binding Path=" + path + @"}'/>
                    </StackPanel>
                  </DataTemplate>"
                );
        }
    this.listBox.ItemTemplate = CreateTemplate(path);
Jim