2017-04-06 1 views
1

新しいカスタムボタンを作成しています。 私はストーリーボードをアニメーションに使用しており、ストーリーボードにはMouseEnter、MouseLeave、MouseDoun、MouseUpイベントが含まれています。 Clickイベントを導入した後MouseDownイベントを停止する作業、MouseUpイベント CustomButtonのXAMLコードがある.....同じ要素にClickイベントを使用した後、MouseDownイベントが発生しません。

<Style TargetType="{x:Type local:FlatButton}"> 
    <Setter Property="Focusable" Value="False" /> 
    <Setter Property="FontFamily" Value="Segoe MDL2 Assets"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
     <ControlTemplate TargetType="{x:Type local:FlatButton}"> 
      <Grid> 
      <Rectangle Name="Part_Rectangle" Fill="{Binding Path=Background}"/> 
      <Label Name="Part_Label" Foreground="White" FontSize="{Binding FontSize, FallbackValue='9'}" 
        HorizontalContentAlignment="Center" VerticalContentAlignment="Center" 
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
        Style="{StaticResource StyleGray}" 
        Content="{Binding Path=Caption, RelativeSource={RelativeSource TemplatedParent}}" 
        FontFamily="{Binding Path=FontFamily, RelativeSource={RelativeSource TemplatedParent}}"/> 
      </Grid> 
     </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    </Style> 

ラベルのスタイルがある.....

<Style x:Key="StyleGray" TargetType="Label"> 
    <Setter Property="Background" Value="#00000000"/> 
    <Style.Triggers> 
     <EventTrigger RoutedEvent="Mouse.MouseEnter"> 
     <BeginStoryboard> 
      <Storyboard> 
      <ColorAnimation Storyboard.TargetProperty="(Label.Background).(SolidColorBrush.Color)" 
             To="#26000000" Duration="0:0:0.15"/> 
      </Storyboard> 
     </BeginStoryboard> 
     </EventTrigger> 
     <EventTrigger RoutedEvent="Mouse.MouseLeave"> 
     <BeginStoryboard> 
      <Storyboard> 
      <ColorAnimation Storyboard.TargetProperty="(Label.Background).(SolidColorBrush.Color)" 
             To="#00000000" Duration="0:0:0.15"/> 
      </Storyboard> 
     </BeginStoryboard> 
     </EventTrigger> 
     <EventTrigger RoutedEvent="Mouse.MouseDown"> 
     <BeginStoryboard> 
      <Storyboard> 
      <ColorAnimation Storyboard.TargetProperty="(Label.Background).(SolidColorBrush.Color)" 
             To="#3F000000" Duration="0:0:0.15"/> 
      </Storyboard> 
     </BeginStoryboard> 
     </EventTrigger> 
     <EventTrigger RoutedEvent="Mouse.MouseUp"> 
     <BeginStoryboard> 
      <Storyboard> 
      <ColorAnimation Storyboard.TargetProperty="(Label.Background).(SolidColorBrush.Color)" 
              To="#26000000" Duration="0:0:0.15"/> 
      </Storyboard> 
     </BeginStoryboard> 
     </EventTrigger> 
    </Style.Triggers> 
    </Style> 
続きます

、ボタンの実装です...背後に

<local:FlatButton Grid.Column="8" x:Name="CloseButton" Caption="&#xE947;" 
           Height="30" Width="45" FontSize="10" 
           ButtonStyle="{StaticResource StyleRed}"/> 

とコードです...

void CloseBtn_Click(object sender, RoutedEventArgs e) 
{ 
    Window window = this.TemplatedParent as Window; 
    if (window != null) 
    { 
    window.Close(); 
    } 
} 

私が間違っていることを教えてください。

答えて

0

私はコメントできないので、答えとして投稿しています。あなたのコードを見ることで、問題はボタン自体からのRountedEventsの処理に関係していると思います。

ラベルのスタイルを使用する代わりに、ControlTemplateトリガーのラベル内のストーリーボードを実行して、ラベルイベントの代わりにボタンイベントをターゲットに設定してみます。

+0

私はすでに同じ問題を試してみました。 –

+1

システムのインポートがカスタムコントロールで正しくないことがわかりました。私は--Controlの代わりにSystem.Windows.Control.Buttonを使用します。しかし、私はコントロールを使用すると私はクリックイベントを使用することはできません。 –

+0

ええ、カスタムボタンを実装するには、Buttonまたは、好ましくはButtonBaseから継承する必要があります。 :) –

関連する問題