在上面 Richard E 的回答中,他指出我不能修改绑定数据,只能修改 XAML 属性(它将传播回绑定数据)。因为我试图修改与触发 MouseDown 事件的矩形不同的矩形中的数据,所以我遇到了很大的困难。我使用 VisualBrush 和 DrawingBrush 将所有功能放入一个矩形中。我将在下面发布生成的 XAML。小心,很长。
<Canvas Background="Transparent">
<Grid>
<Rectangle
Width="40"
Height="40"
RadiusX="5"
RadiusY="5" MouseDown="Rectangle_MouseDown">
<Rectangle.Fill>
<VisualBrush TileMode="None" AlignmentX="Center" AlignmentY="Center">
<VisualBrush.Visual>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.Background>
<DrawingBrush>
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,40,40"/>
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<SolidColorBrush Color="{Binding Source={StaticResource PlaybackResource}, Path=BackColor}"/>
</GeometryDrawing.Brush>
</GeometryDrawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,40,40"/>
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<LinearGradientBrush
StartPoint="0,0.2"
EndPoint="0,0"
Opacity="0.6"
MappingMode="RelativeToBoundingBox">
<GradientStop
Color="Transparent" Offset="0"/>
<GradientStop x:Name="UpperShadingColor"
Color="{Binding Source={StaticResource PlaybackResource}, Path=UpperLeftColor}"
Offset="1"/>
</LinearGradientBrush>
</GeometryDrawing.Brush>
</GeometryDrawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,40,40"/>
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<LinearGradientBrush
x:Name="LeftShading"
StartPoint="0.2,0"
EndPoint="0,0"
Opacity="0.6"
MappingMode="RelativeToBoundingBox">
<GradientStop
Color="Transparent"
Offset="0"/>
<GradientStop x:Name="LeftShadingColor"
Color="{Binding Source={StaticResource PlaybackResource}, Path=UpperLeftColor}"
Offset="1"/>
</LinearGradientBrush>
</GeometryDrawing.Brush>
</GeometryDrawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,40,40"/>
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<LinearGradientBrush
x:Name="LowerShading"
StartPoint="0.8,0"
EndPoint="1,0"
Opacity="0.6"
MappingMode="RelativeToBoundingBox">
<GradientStop
Color="Transparent"
Offset="0"/>
<GradientStop x:Name="LowerShadingColor"
Color="{Binding Source={StaticResource PlaybackResource}, Path=LowerRightColor}"
Offset="1"/>
</LinearGradientBrush>
</GeometryDrawing.Brush>
</GeometryDrawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,40,40"/>
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<LinearGradientBrush
x:Name="RightShading"
StartPoint="0,0.8"
EndPoint="0,01"
Opacity="0.6"
MappingMode="RelativeToBoundingBox">
<GradientStop
Color="Transparent"
Offset="0"/>
<GradientStop x:Name="RightShadingColor"
Color="{Binding Source={StaticResource PlaybackResource}, Path=LowerRightColor}"
Offset="1"/>
</LinearGradientBrush>
</GeometryDrawing.Brush>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentControl Width="30" Height="30" Grid.Column="1" Grid.Row="1" Margin="6,6,6,6"
Content="{Binding Source={StaticResource PlaybackResource}, Path=Symbol}"/>
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</Rectangle.Fill>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="MouseDown">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="UpperShadingColor"
Storyboard.TargetProperty="Color"
To="{Binding Source={StaticResource PlaybackResource}, Path=LowlightColor}"
Duration="0:0:0.01"/>
<ColorAnimation
Storyboard.TargetName="LeftShadingColor"
Storyboard.TargetProperty="Color"
To="{Binding Source={StaticResource PlaybackResource}, Path=LowlightColor}"
Duration="0:0:0.01"/>
<ColorAnimation
Storyboard.TargetName="LowerShadingColor"
Storyboard.TargetProperty="Color"
To="{Binding Source={StaticResource PlaybackResource}, Path=HighlightColor}"
Duration="0:0:0.01"/>
<ColorAnimation
Storyboard.TargetName="RightShadingColor"
Storyboard.TargetProperty="Color"
To="{Binding Source={StaticResource PlaybackResource}, Path=HighlightColor}"
Duration="0:0:0.01"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseUp">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="UpperShadingColor"
Storyboard.TargetProperty="Color"
To="{Binding Source={StaticResource PlaybackResource}, Path=HighlightColor}"
Duration="0:0:0.01"/>
<ColorAnimation
Storyboard.TargetName="LeftShadingColor"
Storyboard.TargetProperty="Color"
To="{Binding Source={StaticResource PlaybackResource}, Path=HighlightColor}"
Duration="0:0:0.01"/>
<ColorAnimation
Storyboard.TargetName="LowerShadingColor"
Storyboard.TargetProperty="Color"
To="{Binding Source={StaticResource PlaybackResource}, Path=LowlightColor}"
Duration="0:0:0.01"/>
<ColorAnimation
Storyboard.TargetName="RightShadingColor"
Storyboard.TargetProperty="Color"
To="{Binding Source={StaticResource PlaybackResource}, Path=LowlightColor}"
Duration="0:0:0.01"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="UpperShadingColor"
Storyboard.TargetProperty="Color"
To="{Binding Source={StaticResource PlaybackResource}, Path=HighlightColor}"
Duration="0:0:0.01"/>
<ColorAnimation
Storyboard.TargetName="LeftShadingColor"
Storyboard.TargetProperty="Color"
To="{Binding Source={StaticResource PlaybackResource}, Path=HighlightColor}"
Duration="0:0:0.01"/>
<ColorAnimation
Storyboard.TargetName="LowerShadingColor"
Storyboard.TargetProperty="Color"
To="{Binding Source={StaticResource PlaybackResource}, Path=LowlightColor}"
Duration="0:0:0.01"/>
<ColorAnimation
Storyboard.TargetName="RightShadingColor"
Storyboard.TargetProperty="Color"
To="{Binding Source={StaticResource PlaybackResource}, Path=LowlightColor}"
Duration="0:0:0.01"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Grid>
</Canvas>