在这里这将起作用,但是请注意,此代码将引发堆栈溢出异常,因为当您将鼠标悬停在红色黑色红色黑色等时,颜色会发生变化:/我只是建议一些其他方法来跟踪矩形状态,可能是 ViewModel 中的一个变量,用于跟踪状态,并在代码中替换:
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Stroke}" Value="Black" />
有类似的东西:
<Condition Binding="{Path=RectState}" Value="True" />
否则会崩溃 :) (抱歉匆忙写这个)
<Rectangle Height="100" HorizontalAlignment="Left" Margin="182,152,0,0" Name="rectangle1" VerticalAlignment="Top" Width="200" >
<Rectangle.Style>
<Style TargetType="Rectangle">
<Setter Property="Fill" Value="Transparent" />
<Setter Property="Stroke" Value="Black" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Stroke}" Value="Black" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="StrokeThickness" Value="2" />
<Setter Property="Stroke" Value="Red" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>