2009-08-27 3 views
7

制御:WPFは - カスタムのカスタム添付イベントを処理する私はそのように宣言したルーティングイベント(名前は無実を保護するために変更されている)している

public class DragHelper : DependencyObject { 
    public static readonly RoutedEvent DragCompleteEvent = EventManager.RegisterRoutedEvent(
     "DragComplete", 
     RoutingStrategy.Bubble, 
     typeof(DragRoutedEventHandler), 
     typeof(DragHelper) 
    ); 

    public static void AddDragCompleteHandler(DependencyObject dependencyObject, DragRoutedEventHandler handler) { 
     UIElement element = dependencyObject as UIElement; 
     if (element != null) { 
      element.AddHandler(DragCompleteEvent, handler); 
     } 
    } 

    public static void RemoveDragCompleteHandler(DependencyObject dependencyObject, DragRoutedEventHandler handler) { 
     UIElement element = dependencyObject as UIElement; 
     if (element != null) { 
      element.RemoveHandler(DragCompleteEvent, handler); 
     } 
    } 

かなり標準のものを。 XAMLでは、私は単一のカスタムコントロールを含むDataTemplateを持っています。このイベント(およびその他の添付プロパティ)をコントロールに添付しようとしています:

<DataTemplate ...> 
    <My:CustomControl 
     My:DragHelper.IsDragSource="True" 
     My:DragHelper.DragComplete="DragCompleteHandler" /> 
</DataTemplate> 

これは、目的の結果が得られません。具体的には、DragCompleteイベントのRaiseEvent()を呼び出すコードが呼び出されても、ハンドラは呼び出されません。実際に、このXAMLファイルのどこかに接続されている他のカスタムルーティングイベントのハンドラもありません。

ルーティングされたイベントの名前を変更しようとしましたが、DataTypeを持つものからx:Keyを持つものにデータテンプレートを切り替えることを試みました。これは行動の目に見える変化をもたらさなかった。

ただし、My:CustomControlをTextBlockなどの組み込みのWPFコントロールに変更すると、イベントは完全に発生します。同様に、自分のカスタムコントロールを自分のプロジェクトの他のカスタムUserControlサブクラスで置き換えた場合、その動作は壊れた、イベントなしの状態に戻ってしまいます。

これは私には全く意味がありません。このシナリオを稼働させるために必要な具体的なことはありますか?それは問題ではないようです。イベントハンドリングを中断させるすべてのカスタムコントロールで特定のことが起こっている可能性があると思いますが、今まで試した3つまたは4つのカスタムコントロールでは何も見たことがありません。

答えて

1

あなたのコードをすべて投稿していないので、自分のバージョンを推測してまとめなければなりません。それは私のために正常に動作します。おそらく、あなたのコードに比較対照:

Window1.xaml.csを:

using System.Windows; 
using System.Windows.Controls; 

namespace WpfApplication1 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
     } 

     private void DragCompleteHandler(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show("YEP"); 
     } 
    } 

    public class CustomControl : TextBox 
    { 
    } 

    public class DragHelper : DependencyObject 
    { 
     public static readonly DependencyProperty IsDragSourceProperty = DependencyProperty.RegisterAttached("IsDragSource", 
      typeof(bool), 
      typeof(DragHelper), 
      new FrameworkPropertyMetadata(OnIsDragSourceChanged)); 

     public static bool GetIsDragSource(DependencyObject depO) 
     { 
      return (bool)depO.GetValue(IsDragSourceProperty); 
     } 

     public static void SetIsDragSource(DependencyObject depO, bool ids) 
     { 
      depO.SetValue(IsDragSourceProperty, ids); 
     } 

     public static readonly RoutedEvent DragCompleteEvent = EventManager.RegisterRoutedEvent(
      "DragComplete", 
      RoutingStrategy.Bubble, 
      typeof(RoutedEventHandler), 
      typeof(DragHelper) 
     ); 

     public static void AddDragCompleteHandler(DependencyObject dependencyObject, RoutedEventHandler handler) 
     { 
      UIElement element = dependencyObject as UIElement; 
      if (element != null) 
      { 
       element.AddHandler(DragCompleteEvent, handler); 
      } 
     } 

     public static void RemoveDragCompleteHandler(DependencyObject dependencyObject, RoutedEventHandler handler) 
     { 
      UIElement element = dependencyObject as UIElement; 
      if (element != null) 
      { 
       element.RemoveHandler(DragCompleteEvent, handler); 
      } 
     } 

     private static void OnIsDragSourceChanged(DependencyObject depO, DependencyPropertyChangedEventArgs e) 
     { 
      (depO as TextBox).TextChanged += delegate 
      { 
       (depO as TextBox).RaiseEvent(new RoutedEventArgs(DragCompleteEvent, null)); 
      }; 
     } 
    } 
} 

Window1.xaml

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfApplication1" 
    Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
     <DataTemplate x:Key="Test"> 
      <local:CustomControl 
       local:DragHelper.IsDragSource="True" 
       local:DragHelper.DragComplete="DragCompleteHandler" /> 
     </DataTemplate> 
    </Window.Resources> 

    <ContentControl ContentTemplate="{StaticResource Test}"/> 
</Window> 
+0

感謝を。 私のカスタムコントロールは、UserControlから派生しましたが、あなたのようなTextBoxに切り替えることさえ、まだ私の問題を示しています。残りのコードは省略しました。なぜなら、名前空間の変更を除いて、基本的にすべてあなたのものと同じであるからです。しかし、それははるかに大きなプロジェクトの一部なので、いくつかのグローバルスタイルやリソースや、それが問題に影響を与える可能性があることに気づいていないものと何らかの相互作用があるかもしれません。私は何ができるかを見ていきます。 –

+1

さて、私はちょっとばかげた。私にとってはうまくいったあなたの完全な例から始めて、私は大きなプロジェクトで見ているような非機能的なケースに変換するために様々なことを試みました。 しばらくすると、問題を再現する基本コードに非常に簡単な変更がありました。私はあなたのDragHelperの実装を取って、メインアプリケーションによって参照され、それに従ってWindow1.xamlを変更する別のアセンブリに移動する場合(xmlns:otherとother:localの代わりにother :)を追加すると、メッセージボックスのfireは表示されなくなります私はテキストボックスに入力します。 :| –

関連する問題