tags:

views:

64

answers:

1

Hello

I have a memorey card game where my binding is to

public ObservableCollection<List<memoryCard>> MyCollection { get; set; }//holding the array 

where is initlized like this :

for (int i = 0; i < Size; i++)
        {
            List<memoryCard> list = new List<memoryCard>();

            for (int j = 0; j < Size; j++)
            {
                var card = new memoryCard(createRandomBrush(array, j));
                card.IsChecked = false;
                list.Add(card);
                card.PropertyChanged += (a, b) => Verify(b.PropertyName);
            }
            MyCollection.Add(list);
        }
**EDIT its now working except to the one EDIT in the middle who do me an hard time **
     private void Verify(string propName)
    {
        if (propName != "IsChecked2")
        {
            return;
        }

       // List<memoryCard> selected = new List<memoryCard>();
        foreach (List<memoryCard> observerListItem in MyCollection)
            foreach (memoryCard mycard in observerListItem)
                if (mycard.IsChecked == true)
                    selected.Add(mycard);

        if (selected.Count == 2)
        {
            if ((selected.First().buttonColor == selected.Last().buttonColor) && 
                (!selected.First().Equals(selected.Last()  )   )   )
            {
                if (firstClick)
                {
                    MessageBox.Show("Good.", "Result", MessageBoxButton.OK, MessageBoxImage.Information);
                    firstClick = !firstClick;
 selected.ForEach(cell => cell.buttonColor = Brushes.White);
                    //here the biding color of the toggle item should chang and it doesnt can someone explain me how to do it right ?ite
                }

                }
            }
            else
            {
                if (firstClick)
                {
                    MessageBox.Show("Bad.", "Result", MessageBoxButton.OK, MessageBoxImage.Error);
                    firstClick = !firstClick;
                }
            }
            selected.First().IsChecked = selected.Last().IsChecked = false;
            selected.Clear();
            firstClick = true;

        }

if (selected.Count!=0)selected.First().IsChecked = false; }

and my memoreycard class is :


 public class memoryCard : INotifyPropertyChanged
{
    #region c'tor
    public memoryCard(Brush _buttonColor)
    {
        buttonColor=_buttonColor;
    }
    #endregion
    private bool ?_isChecked = false;
    public bool ?IsChecked
    {
        get
        {
            return _isChecked;
        }
        set
        { 
            if (_isChecked != value)
            {
                _isChecked = value;
                //OnPropertyChanged("IsChecked");
                OnPropertyChanged("IsChecked2");
            }
        }
    }

    #region colorofbutton

        public Brush buttonColor;
        public Brush ButtonColor
        {
            get
            {
                return buttonColor;
            }
            set
            {
                buttonColor = value;
            }

        }
        #endregion



    #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
}

}

I try to achieve the goal of a memory card game check if there are two card who were IsCheck/ed property value = true and check there color if there's color is equal make this toggle button in a IsCheck/ed property null but the prior algoritam doesnt work!!

this is an example fo what i am trying to achive by the end atoggle button who's changed according to the game EDIT how can I do it using the triggers when all the logic above is working ng ?...

<ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type ToggleButton}">
            <Grid>
                <Ellipse Name="elipse1" Height="65" Width="79" Fill="{Binding Path=ButtonColor}" Visibility="Collapsed"></Ellipse>
                <Ellipse Name="elipse2" Height="65" Width="79" Fill="Black" ></Ellipse>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="elipse1" Property="Visibility" Value="Visible"/>
                    <Setter TargetName="elipse2" Property="Visibility" Value="Collapsed"/>
                </Trigger>

                <Trigger Property="IsCheck" Value="null">
                    <Setter TargetName="elipse1" Property="Visibility" Value="Collapsed"/>
                    <Setter TargetName="elipse2" Property="Visibility" Value="Collapsed"/>
                </Trigger>


            </ControlTemplate.Triggers>
        </ControlTemplate>

** EDIT**

so to summrize i have two questiones about my code

  1. i changed the card color to white in verfy why doesnt the toggle button associted to him changes his color as well?
  2. how can i trugger a toggle button to be allways in one color? when a card gets new color ,White i want him to change his color to ehite how can i achieve it ?

    thank you very much.

EDIT2

my only problem is thatbindingto the color and changing the color like this :

     selected.ForEach(cell => cell.buttonColor = Brushes.White);

doesnt make the ui notice it , even thought the card property changed and call

  OnPropertyChanged("ButtonColor");

the UI doesnt change it

EDIT3

now i want a triger that whent i am on it will give aany toggle button who have white color the white color

       <ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type ToggleButton}">
            <Grid>
                <Ellipse Name="elipse1" Height="65" Width="79" Fill="{Binding Path=ButtonColor}" Visibility="Collapsed"></Ellipse>
                     <Ellipse Name="elipse2" Height="65" Width="79" Fill="Black" ></Ellipse>
     <Ellipse Name="elipse3" Height="65" Width="79" Fill="Black" Visibility="Collapsed" ></Ellipse>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="elipse1" Property="Visibility" Value="Visible"/>
                    <Setter TargetName="elipse2" Property="Visibility" Value="Collapsed"/>
                </Trigger>

                <Trigger Property="IsCheck" Value="null">
                    <Setter TargetName="elipse1" Property="Visibility" Value="Collapsed"/>
                    <Setter TargetName="elipse2" Property="Visibility" Value="Collapsed"/>
        <Setter TargetName="elipse3" Property="Visibility" Value="Visible"/>

                </Trigger>
+1  A: 

When you set your ButtonColor, call PropertyChanged. So:

#region colorofbutton

    private Brush buttonColor; // Use the property accessors instead
    public Brush ButtonColor
    {
        get
        {
            return buttonColor;
        }
        set
        {
            buttonColor = value;
            OnPropertyChanged("ButtonColor");
        }

    }
    #endregion
Lunivore
it seems like a good idea but it doesnt work ... the color on the card himself when i debug i see that the card woh is refrenced to the button change his name but the the button remain wwith the same color
yoav.str
You also need to call `ButtonColor`, not `buttonColor`. Make that field private and you'll see where you're using it (`cell => cell.**B**uttonColor = Brushes.White`). I've edited the code above to show you what I mean; sorry I didn't spot that before.
Lunivore
@Lunivore it WORKS but just for ONE of them and not foreach can you explain me why ? how can i change it to work for them both ?.
yoav.str
I'm not completely sure what you mean, but if you mean that both ellipses should be white, then here's your problem: `Fill="Black"` instead of `Fill="{Binding Path=ButtonColor}"` on the 2nd Ellipse. Otherwise, please can you explain what things aren't white and should be?
Lunivore
they are both white my problem now is when the color set to ehite i want this eclipse to turn white .what do you think will help me achive it maybe all my concept is wrong
yoav.str
I can't really help with your specific code because I don't understand what it is you're trying to do, and I'm afraid your question above doesn't make much sense to me. However: check that whenever you change something you're calling `PropertyChanged`. Check that you're accessing things through properties - make all your fields private.
Lunivore