Get the object that a custom control is bound to when in controls?

I have a parent user control, here is an excerpt from 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>

      

inside this usercontrol (one is added for each item in the watched collective being loaded). I have several Downloader: Download User Controls (as can be seen above in the item template) that have the following xaml in the notification: percentage binding complete and 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"/>

      

it all works fine, i add item to collection where ui picks up new item, add new control, sweet. the problem is I want to get the associated object in the Child control in code, so I can call methods on it, I just can't figure out how to do it, in the code for the child controls id to be able to do something like this

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

        object DownloadEntity = this.DataContext as DownloadEntity;

    }

      

but no datacontext ...

Is there a way to get the "object" that my child custom element is bound to?

Thank!

+2


source to share


1 answer


as I have now found that if I handle events on controls in xaml the dataset is set and can be retrieved like this

MyDataBoundEntity mdbe = this.DataContext as MyDataBoundEntity;

      



easy as

+2


source







All Articles