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=""
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();
}
}
請告訴我我做錯了什麼。
我已經試過這個問題是一樣的。 –
我發現我的系統導入對於自定義控件不正確。我使用 - System.Windows.Control.Button而不是--Control。但是,當我使用控制我不能使用點擊事件。 –
是的,要實現一個自定義按鈕,你應該從Button繼承,或者最好是ButtonBase。 :) –