WPF Controls Dll question

wpf

I've started work on a WPF controls dll which will be referenced from winforms applications.

My first control in this project is a simple container control. It's purpose in life is to provide a modaless, transparent docking window to the Winforms app.

My problem with this control is that I can't get it to move when I try to drag it. I can move it if I don't put it into a .dll. Inside the dll, Canvas.GetLeft – returns an invalid number, and I don't know how to fix this.

Here's the bulk of the XAML for this control:

    <Grid Height="Auto">
    <Grid.Resources>
        <LinearGradientBrush x:Key="BackBrush" EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#C4000000"/>
            <GradientStop Offset="1" Color="#66000000"/>
            <GradientStop Color="#61000000" Offset="0.50400000810623169"/>
        </LinearGradientBrush>
    </Grid.Resources>

    <Border Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Margin="0,0,0,0" BorderBrush="#FF000000" BorderThickness="2,2,2,2" Background="{StaticResource BackBrush}" Opacity="1" CornerRadius="8,8,8,8">
        <StackPanel Background="{x:Null}" Opacity="1" Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Top" x:Name="spWhatAreYouDoing">
            <ContentControl></ContentControl>
        </StackPanel>
    </Border>
    <Thumb Background="{x:Null}" Opacity="0" DragDelta="onDragDelta" x:Name="panelthumb"/>

</Grid>

I've hooked the Thumb's onDragDelta event in the code behind and use that to drag this window around. This works fine when I use it in the same .exe.

public void onDragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
    {
        Canvas.SetLeft(this, Canvas.GetLeft(this) + e.HorizontalChange);
        Canvas.SetTop(this, Canvas.GetTop(this) + e.VerticalChange);
    }

When I take this same code out of the .exe and place it into a .dll then reference that .dll and use the control from my exe – it won't drag anymore. The window displays but won't move.

The reason it won't be moved is because Canvas.GetLeft(this) is returning an invalid number. Why? Although the destination for this control is usage by a Winforms app, I find this same behavior when I use it from a WPF app without any ElementHost intervention.

Edit – when I host this control directly within my Winforms app using ElementHost I can move the window. But window transparency is lost. And so the entire reason for moving to WPF for this form is invalid.

So I have done this wrong – what is the right way to call GetLeft from within a control hosted from a .dll?

Best Answer

I suggest you first host the WPF control inside a WinForm. Disable all the border properties of the WinForm...so your control looks as if it is all by itself. Then use this WinForm as the primary means of using your control.

This way you get access to all the related functionality.

In a gist >>> XAML inside a standalone Winforms host. >>> Winform to be used as control in other Winforms.