我有一个纪念卡片游戏,我的装订是
public ObservableCollection<List<memoryCard>> MyCollection { get; set; }//holding the array
这里是initlized:
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
}
}
我试图实现记忆卡游戏的目标 检查是否有两张卡是IsCheck / ed属性值= true并检查颜色是否相等使得IsCheck / ed属性中的此切换按钮为null 但以前的algoritam不起作用!!
这是一个例子,我试图通过最终的atoggle按钮来实现根据游戏改变 的修改 当上述所有逻辑都工作时,如何使用触发器来实现?...
<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>
**编辑**
所以总结一下,我有两个关于我的代码的问题
如何将切换按钮强制为一种颜色?当一张卡片获得新颜色时,白色我希望他改变颜色,以便我能够实现它吗?
非常感谢你。
EDIT2
我唯一的问题是绑定到颜色并改变颜色如下:
selected.ForEach(cell => cell.buttonColor = Brushes.White);
并没有让ui注意到它,甚至认为卡片属性发生变化并致电
OnPropertyChanged("ButtonColor");
用户界面不会改变它
EDIT3
现在我想要一个triger,当我在它上面时会给出一个白色的白色切换按钮
<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>
答案 0 :(得分:1)
设置ButtonColor
后,请致电PropertyChanged
。所以:
#region colorofbutton
private Brush buttonColor; // Use the property accessors instead
public Brush ButtonColor
{
get
{
return buttonColor;
}
set
{
buttonColor = value;
OnPropertyChanged("ButtonColor");
}
}
#endregion