www.planet-xaml.net
 
  • [WPF]

    More Visual Feedback for Drag'n'Drop

    by Florian Krüsch, July 20, 2007

    Jaime's always a good source for some real world stuff.
    His series of blog posts on drag'n'drop is a must read:


    Drag and drop with custom visuals across windows... yeah :) I found out you can also use a Popup control to do the trick, which is the WPF way of rendering visuals to independent HWNDs:

    C#CursorBoundPopup.cstoggle
    public class CursorBoundPopup : Popup
    {
       protected override void OnOpened(EventArgs e)
       {
          base.OnOpened(e);
          CompositionTarget.Rendering += CT_Rendering;
       }

       protected override void OnClosed(EventArgs e)
       {
          CompositionTarget.Rendering -= CT_Rendering;
          base.OnClosed(e);
       }

       private void CT_Rendering(object sender, EventArgs e)
       {
          Win32.POINT p;
          if (!Win32.GetCursorPos(out p)) return;

          HorizontalOffset = (Double)p.X;
          VerticalOffset = (Double)p.Y;
       }
    }

    Nice thing is, you can now style the dragged visual declaratively and use databinding for the visual brush:

    XAMLtoggle
    <dd:CursorBoundPopup xmlns:dd="clr-namespace:DragAndDropWithGhost"
                   x:Name="dragPopup" 
                   IsOpen="False"
                        
    AllowsTransparency="True"
                        
    HorizontalAlignment="Left" VerticalAlignment="Top"
                        
    Placement="Absolute"
                        
    PopupAnimation="Fade"
        
    >
          
    <Rectangle Width="{Binding Width,ElementName=dragElement}"
                     
    Height="{Binding Height,ElementName=dragElement}"
          
    >
            
    <Rectangle.Fill>
              
    <VisualBrush Visual="{Binding ElementName=dragElement}" 
                      
    Opacity="0.5" />
            
    </Rectangle.Fill>
          
    </Rectangle>
        
    </dd:CursorBoundPopup>

    The start drag routine (a little oversimplified maybe) then looks like this:

    C#StartDragWithPopuptoggle
    private void StartDragWithPopup(MouseEventArgs e)
    {
        
    // prepare D&D data
        DataObject data = new DataObject(System.Windows.DataFormats.Text.ToString(), 
    "abcd");

        
    // prepare D&D
        DragSource.GiveFeedback += DragSource_GiveFeedback;
        dragPopup.IsOpen = true;

        
    // do D&D
        try
        {
       DragDropEffects de = DragDrop.DoDragDrop(this.DragSource, data, DragDropEffects.Move);
        }
        
    // I got COM interop exceptions from time to time... so 
        catch { }

        
    // done with D&D            
        DragSource.GiveFeedback -= DragSource_GiveFeedback;
        dragPopup.IsOpen = false;
    }

    cheers,
    Florian


    no comments
  •  ^
  • [IronRubyDLRWPF]

    Scott Hanselmann und IronRuby

    by Florian Krüsch, July 24, 2007

    Scott Hanselmann, Alpha-Geek und bekennender Ruby-Fan wechselt zu Microsoft.

    Ich habe keine Ahnung, was hinter Wesabe steckt, aber dass ich mich jetzt noch ewas tiefer mit meiner neuen "Hobby-Sprache" Ruby beschäftigen kann, ohne meine kuschelige .NET/WPF/ASP.net Ecke zu verlassen, ist schon fein.

    WPF ist zwar noch etwas mühsam, aber für die Zukunft lässt sich absehen, wie sich WPF Apps aus performace-kritischen Teilen in C# und rails-ähnlichen Ruby Teilen zusammenstöpseln lassen werden.

    cheers,
    Florian


    no comments
  •  ^
 

Language | Sprache

All[e]  |  english  |  deutsch
Tags
 

Profile

Florian Kruesch

I am working as a freelance software architect, developer and consultant in Düsseldorf, Germany.

My focus is on Microsoft technologies and .NET 3.0, especially WPF, ASP.net and SQL Server.

I've been a lead programmer on the WPF development of the OTTO Vista Store at SinnerSchrader Studios.

My client list includes DHL, LG and Ogilvy Interactive.