我创建了一个类似于按钮的用户控件(参见下面的代码)。
为了获得用户控制动画,我使用了EventTriggers鼠标事件(Enter / Leave,Down / Up)。
在我将InputBinding添加到MouseClick
之后<Border.InputBindings>
<MouseBinding Command="{Binding ClickCommand}" MouseAction="LeftClick" />
</Border.InputBindings>
MouseDown的动画再也不起作用了。有关如何添加ClickCommand并保留动画的任何建议吗?
<UserControl x:Class="DauertestViewer.StationItemControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DauertestViewer"
mc:Ignorable="d"
d:DesignHeight="150" d:DesignWidth="150">
<UserControl.Resources>
<Style x:Key="ContentStyle" TargetType="{x:Type ContentControl}">
<Setter Property="Background" Value="White" />
<Setter Property="Foreground" Value="#363637" />
<Setter Property="BorderThickness" Value="5" />
<Setter Property="FontSize" Value="14" />
<Setter Property="Height" Value="150" />
<Setter Property="Width" Value="150" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}" >
<Border x:Name="border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True"
CornerRadius="0"
Margin="10">
<Border.Effect>
<DropShadowEffect
x:Name="dropshadow"
ShadowDepth="4"
Direction="300"
Color="#363637"
Opacity="0.4"
BlurRadius="4"/>
</Border.Effect>
<Border.InputBindings>
<MouseBinding Command="{Binding ClickCommand}" MouseAction="LeftClick" />
</Border.InputBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Viewbox Grid.Row="0" Margin="5 8 5 5" >
</Viewbox>
<TextBlock Grid.Row="1"
Margin="0 0 0 2"
Text="{Binding StationName, FallbackValue=Dauertest}"
FontFamily="{TemplateBinding FontFamily}"
Foreground="{TemplateBinding Foreground}"
FontSize="{TemplateBinding FontSize}"
FontWeight="Regular"
TextWrapping="Wrap"
TextAlignment="Center"
VerticalAlignment="Center" >
</TextBlock>
</Grid>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="MouseEnter" SourceName="border">
<BeginStoryboard>
<Storyboard TargetName="dropshadow">
<DoubleAnimation
Storyboard.TargetProperty="BlurRadius"
Duration="0:0:0.2"
From="4"
To="10" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave" SourceName="border">
<BeginStoryboard>
<Storyboard TargetName="dropshadow">
<DoubleAnimation
Storyboard.TargetProperty="BlurRadius"
Duration="0:0:0.2"
From="10"
To="4" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseDown" SourceName="border">
<BeginStoryboard>
<Storyboard TargetName="border">
<ThicknessAnimation
Storyboard.TargetProperty="Margin" Duration="0:0:0.2"
From="10,10,10,10" To="10,10,15,15" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseUp" SourceName="border">
<BeginStoryboard>
<Storyboard TargetName="border">
<ThicknessAnimation
Storyboard.TargetProperty="Margin" Duration="0:0:0.2"
From="10,10,15,15" To="10,10,10,10" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<ContentControl Style="{StaticResource ContentStyle}"/>
ViewModel:
using System.Net;
using System.Net.NetworkInformation;
using System.Windows.Input;
namespace DauertestViewer
{
public class StationItemViewModel : BaseViewModel
{
public StationItem StationItem { get; set; }
public string IconColor { get; set; }
public string IconName { get; set; }
public ICommand ClickCommand { get; set; }
public bool UpdateConnectionStatusInProgress { get; set; }
public ConnectionStatus CurrentConnectionStatus { get; set; }
public StationItemViewModel()
{
ClickCommand = new RelayCommand(() => PingIpAddress(IPAddress.Parse("192.168.0.230")));
}
private async void PingIpAddress(IPAddress StationIP)
{
await RunCommand(() => this.UpdateConnectionStatusInProgress, async () =>
{
Ping pingSender = new Ping();
PingReply pingReply = await pingSender.SendPingAsync(StationIP, 10000);
if (pingReply.Status == IPStatus.Success)
{
this.CurrentConnectionStatus = ConnectionStatus.Connected;
}
else
{
this.CurrentConnectionStatus = ConnectionStatus.NotConnected;
}
});
}
}
}
如果我在构造函数中注释掉ClickCommand,一切正常 - 它与RelayCommand本身有关......