2017-02-07 9 views
0

私は単純なコントローラを作成しようとしています。ここには私のGeneric.xamlがあります。カスタムコントロールでイベントを処理する方法

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:TopluNotEkle"> 


    <Style TargetType="{x:Type local:FileSelector}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type local:FileSelector}"> 

        <Button BorderThickness="8,8,8,8" Background="Gray" x:Name="MainButton"> 
         <Button.BorderBrush> 
          <DrawingBrush TileMode="Tile" Viewport="0,0,64,64" ViewportUnits="Absolute"> 
           <DrawingBrush.Drawing> 
            <DrawingGroup> 
             <GeometryDrawing Brush="LightGray"> 
              <GeometryDrawing.Geometry> 
               <RectangleGeometry Rect="0,0,100,100" /> 
              </GeometryDrawing.Geometry> 
             </GeometryDrawing> 
             <GeometryDrawing Brush="DarkGray"> 
              <GeometryDrawing.Geometry> 
               <GeometryGroup> 
                <RectangleGeometry Rect="0,0,50,50"/> 
                <RectangleGeometry Rect="50,50,50,50"/> 
               </GeometryGroup> 
              </GeometryDrawing.Geometry> 
             </GeometryDrawing> 
            </DrawingGroup> 
           </DrawingBrush.Drawing> 
          </DrawingBrush> 
         </Button.BorderBrush> 
         <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="BlueViolet" FontWeight="Bold" FontSize="20">Drag & Drop</TextBlock> 
        </Button> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

、これは私がThe name MainButton does not exist in current contextエラーを取得しています

public class FileSelector : Control 
{ 
    static FileSelector() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(FileSelector), new FrameworkPropertyMetadata(typeof(FileSelector))); 
     MainButton.Drop += myDrop; 

    } 

    static void myDrop(object sender, DragEventArgs e) 
    { 
     Debug.Fail("This is called"); 
    } 
} 

の背後に私のコードです。私もDrop = "myDrop"を設定しようとしましたが、それもうまくいきませんでした。

コンポーネントのイベントをどのように聞くことができますか?

答えて

0

ボタンがx:Name属性によって名前が割り当てられていますクリックイベントハンドラをフックアップします。これは、カスタムコントロールクラスのOnApplyTemplateをオーバーライドすることによって行われます。 http://blog.magnusmontin.net/2013/03/16/how-to-create-a-custom-window-in-wpf/

は、この例の(必ずしもそうではないが)のような完全なチュートリアルに従ってくださいテンプレート

public override void OnApplyTemplate() 
{ 
    Button mainButton = GetTemplateChild("MainButton") as Button; 
    if (mainButton != null) 
     mainButton.Click += MainBtnClick; 
} 

からボタンを取得します

0

あなたの問題は、 'Drop'値として設定されているものではなく、利用できないMainButtonへの参照です。

あなたはUserControlとして定義されたカスタムコントロールを作成しようとしていると思いますが、ResourceDictionaryの一部ではありません。 WPFで数年間働いていないので間違っているかもしれません。ここでは、ユーザーコントロールを作成する方法である:

<UserControl x:class= "YourNamespace.FileSelector" ... other xmlns garbage> 

<Button BorderThickness="8,8,8,8" Background="Gray" x:Name="MainButton"> 
    <Button.BorderBrush> 
     <DrawingBrush TileMode="Tile" Viewport="0,0,64,64" ViewportUnits="Absolute"> 
      <DrawingBrush.Drawing> 
       <DrawingGroup> 
        <GeometryDrawing Brush="LightGray"> 
         <GeometryDrawing.Geometry> 
          <RectangleGeometry Rect="0,0,100,100" /> 
         </GeometryDrawing.Geometry> 
        </GeometryDrawing> 
        <GeometryDrawing Brush="DarkGray"> 
         <GeometryDrawing.Geometry> 
          <GeometryGroup> 
           <RectangleGeometry Rect="0,0,50,50"/> 
           <RectangleGeometry Rect="50,50,50,50"/> 
          </GeometryGroup> 
         </GeometryDrawing.Geometry> 
        </GeometryDrawing> 
       </DrawingGroup> 
      </DrawingBrush.Drawing> 
     </DrawingBrush> 
    </Button.BorderBrush> 
    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="BlueViolet" FontWeight="Bold" FontSize="20">Drag & Drop</TextBlock> 
</Button> 

また、このチュートリアルをチェックアウト:私たちは、コードでそれらを見つけることができるようにするためにhttps://www.tutorialspoint.com/wpf/wpf_custom_controls.htm

関連する問題