views:

32

answers:

1

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=ButtonColor}"-->
  </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)

A: 

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.

Tri Q
you deduced wrong ... i want tthe most cose in my xmal while using ?MVVM...
yoav.str
if you're heading down the MVVM road, then i would not expect to see event handler such as Button.Click instead you should use command binding. See edit answer for MVVM approach.
Tri Q