views:

123

answers:

1

i have a parent usercontrol, here is an excerpt of xaml

<Grid x:Name="LayoutRoot" Width="Auto" Height="Auto" Background="Black" >
    <ItemsControl ItemsSource="{Binding Path=Downloads, Source={StaticResource theViewModel}}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Downloader:DownloadControl DataContext="{Binding}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>          
</Grid>
</UserControl>

within this usercontrol (one is added per item in the downloads observable collectio) i have multiple Downloader:Download User controls (as can be seen above in the item template) which have the following xaml within notice the binding to percentage complete and the datacontext..

</Grid.RowDefinitions>
    <ProgressBar x:Name="progressbar" IsIndeterminate="False" Minimum="0"        
Maximum="100"     Value="{Binding PercentageComplete}" DataContext="{Binding}" 
Height="20"     HorizontalAlignment="Left" Margin="69,35,0,0" VerticalAlignment="Top" 
Width="600" Foreground="#FF20B802"/>

this all works great, i add an item to the collection the UI picks up the new item, adds a new control, sweet. the problem is, i want to be able to get the the bound object in the Child user control in codebehind, so i can call methods on it, i just cant seem to figure out a way of doing it, in codebehind for the child controls id like to be able to do something like this

    public DownloadControl()
 {
  // Required to initialize variables
  InitializeComponent();

        object DownloadEntity = this.DataContext as DownloadEntity;

    }

but there is no datacontext...

is there any way to get the "object" that my child user control is bound to?

Thanks!

A: 

as i have now discovered if i handle events on controls in the xaml, the datacontext is set and can be derived as follows

MyDataBoundEntity mdbe = this.DataContext as MyDataBoundEntity;

easy as that

Matt