Can't find windows control object

Jun 14, 2010 at 8:40 AM
Edited Jun 14, 2010 at 2:55 PM

I would like to change a URL for image control - imgSmall1, so I try to do "imgSmall1.Source = new BitmapImage(new Uri(GetParam("Image1"), UriKind.Absolute));" on MainPage()

However, I can't access imgSmall1 as it is a null object, what even using (Image)this.FindName("imgSmall1"), also a null object

Please provide a solution to solve the problem.


         <dragdrop:DropTarget ShowHover="False" >
                        <dragdrop:DragSource x:Name="dragSource_imgSmall1" AllDropTargetsValid="True" Height="50" Width="50" BorderThickness="0"
                                         DropMode="ReturnDragSource" DragHandleMode="FullDragSource" MouseEnter="imgSmall1_MouseEnter" MouseLeave="imgSmall1_MouseLeave" MouseLeftButtonDown="imgSmall1_MouseLeftButtonDown" MouseLeftButtonUp="imgSmall1_MouseLeftButtonUp">
                       <Image x:Name="imgSmall1" Grid.Row="1" Grid.Column="1" Height="50" Width="50" RenderTransformOrigin="0.5,0.5" Source="Ent3Small.png" Stretch="Fill" Canvas.ZIndex="1">
                <Image x:Name="imgSmallGhost1" Stretch="Fill" Height="50" Width="50"   Grid.Row="1" Grid.Column="1" />

Jun 17, 2010 at 7:26 AM



if you need access to "imgSmall1", the easiest way to get is is by adding a loaded-handler to the image.  In that loaded-handler, you can get the sender and keep a reference to it as needed.


However, as a general "rule"/best practice in XAML programming, you should never need to access any visual element directly.  Use datacontext & bindings instead.  If you want to know more about that, look around for the MVVM design pattern - the internet is full of posts about it :-)


Kind regards,