views:

44

answers:

0

Hi!

I have a UserControl that contains another UserControl. Like this:

<HeaderedContentControl Grid.Row="2"
    Header="New A"
    DataContext="{Binding NewA}"
    Template="{StaticResource NewAResource}">

The outer UserControl has elements which I can drag. The embedded UserControl has a TextBox to which I want those elements to be dragged on.

<TextBox Text="{Binding Name}"
    x:Name="BTextBox"
    AllowDrop="True"/>

I have the code going, if I do not use the embedded UserControl via TemplateBinding, however, I'd really like to have these things as seperated Controls.

Now I tried different things to achieve this. First I tried this:

<TextBox Text="{Binding Name}"
   x:Name="BTextBox"
   AllowDrop="True" 
   PreviewDragOver="{Binding UpperUserControl.TextBox_PreviewDragOver}"
   PreviewDrop="{Binding UpperUserControl.OnDataDropped}" />

This results with a "Only instance methods on the generated or code-behind class are valid." exception.

Then I tried using the RelativeSource Binding, with the same result.

Then I tried using Setters, calling the Name of the TextBox (ParentTextBox), however, you can't use Setters in Styles and with TargetNames. See this link: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7d4bfb6c-b8b0-42aa-beb3-528e19ef0896

Then I tried fiddling around with the Code-Behind as suggested in the link. But with no success. Neither can I find the UpperUserControl from within the embedded Usercontrol, so I cannot access the EventHandlers:

this.BTextBox.PreviewDragOver += Helper.FindParent<UserControl>(this).OnDataDropped;

The FindParent method always returns null, it did work before in another project though. I guess it has problems, since the UserControl is within a template.

Alternatively calling this.Parent also returns null in the embedded UserControl.

Nor can I find the embedded Usercontrol from the UpperUserControl's codebehind, so I can't add the Eventhandlers to the TextBox:

this.BTextBox.PreviewDragOver += TextBox_PreviewDragOver;
this.BTextBox.PreviewDrop += OnDataDropped;

I reckon one of the latter two ways can be made to work, but I don't know how. Can somebody lead me to the solution?