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
- i changed the card color to white in verfy why doesnt the toggle button associted to him changes his color as well?
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>