2
votes

How can I pass the DataContext of LayoutRoot to the converter of the ContentControl inside the ListBox items template?

<Grid x:Name="LayoutRoot"
  Background="White"
  DataContext="{Binding Source={StaticResource myViewModel}}">
    <StackPanel HorizontalAlignment="Left"
          Margin="6,6,0,394"
          Orientation="Vertical"
          Width="200"
          d:LayoutOverrides="Height">
    <ListBox x:Name="listBox2"
         ItemsSource="{Binding MyCollection, Mode=TwoWay}"
         VerticalAlignment="Top"
         Height="400">

       <ListBox.ItemTemplate>
          <DataTemplate>
             <ContentControl Content="{Binding}"
                      ContentTemplate="{Binding Converter={StaticResource  myConverter}}"/>
          </DataTemplate>
       </ListBox.ItemTemplate>
      </ListBox>
     <TextBox Text="{Binding Value1, Mode=TwoWay}"/>
    </StackPanel>
</Grid>

I want to be able to touch the objects inside the DataContext from within the Converter and use them for TwoWay binding on controls within the ListBox item's DataTemplate.

Any ideas? Any suggestions?

Thank you.

2

2 Answers

2
votes

I just got the DataContext up to the converter using:

<ContentControl Content="{Binding}"
                ContentTemplate="{Binding Converter={StaticResource stringToDataTemplateConverter}, ConverterParameter={StaticResource myViewModel}}" />

Now I have another problem my dynamic property binding is not working.

0
votes

(sorry for my bad english)

I'm not sure about what you are trying to do here but with SL 5 you can use RelativeSource to get the DataContext:

{Binding DataContext,RelativeSource={RelativeSource AncestorLevel=1,AncestorType=Grid}}