开发者

wpf how to disable the default animation when button is pressed

开发者 https://www.devze.com 2023-01-19 03:44 出处:网络
I have a memory card game where i want on clicking on a button to bind the prorty button background to

I have a memory card game where i want on clicking on a button to bind the prorty button background to this is what i am doing:

<Button 
  Name="btn"
  Click="Button_Click"
  DataContext="{Binding}"
  Height="65" Width="79"
  Background="Black"/>

<DataTemplate.Triggers>
  <Trigger SourceName="btn" Property="IsMouseCaptured" Value="True">
    <Setter TargetName="btn" Property="Background" Value="Green"/>
    <!--"{Binding Path=Butto开发者_JAVA技巧nColor}"-->
  </Trigger>
</DataTemplate.Triggers>

for a reason I'm too ignorant to understand it doesn't work (notice that the binding itself does works when i use the binding in the defult state)


It seems if you are going to go an all "code-behind" approach, why even bother with the triggers?

Just set btn.Background = new SolidColorBrush(Colors.Green); in your click handler and you're done.

EDIT:

Basically your template trigger is working, but not the way you are expecting it to. If you did some isolation testing, you'll see that the background does turn green when you click on it, but immediately changes back to the original color.

What you're better off doing is using a ToggleButton. This way, you can use the IsChecked property to set the background color. The trick here is to change it back when some other toggle is clicked. This is where your MVVM comes in.

Scenario:

Your View-Model will have a collection of say ... "Cards" and each card can have a property to represent if it has been "turned up". Now, on the event that you have turned up a card, your View-Model should cycle through your "Cards" collection and set all "Card" to being "turned up" to false except for the one card that is "turn up" of course. Now bind the ToggleButton.IsChecked property to the "turned up" property for each "Card" model it binds to.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号