DropTarget Content

Sep 25, 2009 at 2:19 PM

Hello Kevin,

I have a problem when I try add objects within DragSource. If you look in codebehind, lblname or other object that I need appears as null, and I don't understand what happen. Maybe I'm wrong, because I'm newly in Silverlight. You can help me?

XAML code:

<dd:DropTarget GhostVisibility="Visible"> 
	<dd:DragSource AllDropTargetsValid="true" AutoFitGhost="true" ShowSwitchReplaceAnimation="False" DragHandleMode="FullDragSource"> 
	<Border x:Name="brdLowerRectangle" CornerRadius="8,8,8,8" BorderThickness="1" BorderBrush="#19000000" VerticalAlignment="Center" HorizontalAlignment="Center"> 
	<Border CornerRadius="7,7,7,7" BorderThickness="1" BorderBrush="#33000000"> <Border x:Name="brdHover" CornerRadius="6,6,6,6" BorderThickness="1">
	<Border x:Name="brdMain" CornerRadius="5,5,5,5" BorderThickness="1" BorderBrush="#80000000">
		<TextBlock x:Name="lblName" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="15,5,15,5" />

</Border> </Border> </Border> </Border> </dd:DragSource> </dd:DropTarget.Content> </dd:DropTarget> 



public partial class NodeContent : UserControl
	public NodeContent(Node current) {

		this.Width = current.Width;
		this.Height = current.Height;
        	lblName.Text = "hello world"; /* lblName or other object as brdMain is Null */ 




Sep 25, 2009 at 2:53 PM


what you need is a reference to your labels/borders/etc.  There's a little trick to get this: handle the loaded-event of the frameworkelements you need to save a reference to, and keep the sender of that loaded-event for easy access later on. 

Like this:

<TextBlock x:Name="lblName" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="15,5,15,5" Loaded="LoadedHandler" />

Then, in the eventhandler LoadedHandler, cast the sender to "TextBlock", and keep it in a variable called "myLabel".  From then on, you can access the textblock at anytime by using "myLabel".

However, as a general rule: in Silverlight, you should try to avoid having to do this: most of the cases where you would need a reference to an object in your visual tree, you can get the element from the sender-object in any handler.  If you need to get the element to set one of its properties, like .Text, try using an underlying object as datacontext, bound to the TextBlock, and change that underlying object instead of refering to the TextBlock itself.

Hope this helps!




Sep 28, 2009 at 12:20 AM

Hello Kevin,


You know that your solution work fine in a blank project, but not work in my project: an organization chart that should allow drag and drop his rectangles.

From days ago I'm tr ying solved but haven't lucky. :(

What I do make wrong?

Sep 28, 2009 at 12:30 AM

I was trying all ways... I discover that in my proyect, after droptarget, the dropsource not find it despite are in xaml. anyway I trying putting objects (dragsource, then rectangle, etc) in
droptarget.content from c# code in codebind, but not work nor.

Oct 14, 2009 at 10:14 AM

That's very weird...  if the solution works in a new project, I'd be inclined to think something else is wrong...  I don't know if it's possible, but maybe you can start with a blank project and keep on adding the items/code until it resembles your current project?  If all else fails, that's a method I tend to use to try and locate an error...