记忆卡游戏WPF问题 [英] memory card game wpf problem
问题描述
我有一个memorey纸牌游戏,我的结合是
公开的ObservableCollection<名单,LT; MEMORYCARD>> MyCollection的{搞定;组; } //抱着阵列
其中initlized这样的:
的for(int i = 0; I<尺寸;我++)
{
清单<&MEMORYCARD GT;名单=新名单,LT; MEMORYCARD>(); 对于(INT J = 0; J<尺寸; J ++)
{
VAR卡=新MEMORYCARD(createRandomBrush(数组,J));
card.IsChecked = FALSE;
list.Add(卡);
card.PropertyChanged + =(A,B)=>验证(b.PropertyName);
}
MyCollection.Add(名单);
}
**编辑它现在的工作除了中间的一个编辑谁做我一个困难时期**
私人无效验证(字符串作为propName)
{
如果(作为propName!=IsChecked2)
{
返回;
} //列表<&MEMORYCARD GT;选择=新的List<&MEMORYCARD GT;();
的foreach(列表<&MEMORYCARD GT; observerListItem MyCollection中)
的foreach(在observerListItem MEMORYCARD mycard)
如果(mycard.IsChecked ==真)
selected.Add(mycard); 如果(selected.Count == 2)
{
如果((selected.First()buttonColor == selected.Last()buttonColor)及。&放大器;
(!selected.First()。等于(selected.Last())))
{
如果(firstClick)
{
的MessageBox.show(好,结果,MessageBoxButton.OK,MessageBoxImage.Information);
firstClick = firstClick!;
selected.ForEach(电池=> cell.buttonColor = Brushes.White);
//这里的切换项目的招投标颜色应昌它不能够有人解释我怎么做是正确的?伊特
} }
}
其他
{
如果(firstClick)
{
的MessageBox.show(坏,结果,MessageBoxButton.OK,MessageBoxImage.Error);
firstClick = firstClick!;
}
}
selected.First()=器isChecked selected.Last()=器isChecked假。;
selected.Clear();
firstClick = TRUE; }
如果(selected.Count!= 0)selected.First()=器isChecked假。
}
和我memoreycard类是:
公共类MEMORYCARD:INotifyPropertyChanged的
{
#区域c'tor
公共MEMORYCARD(刷_buttonColor)
{
buttonColor = _buttonColor;
}
#endregion
私人布尔_isChecked =假?;
公共BOOL?器isChecked
{
得到
{
返回_isChecked;
}
组
{
如果(_isChecked!=值)
{
_isChecked =价值;
// OnPropertyChanged(器isChecked);
OnPropertyChanged(IsChecked2);
}
}
} #区域colorofbutton 公共刷buttonColor;
公共刷ButtonColor
{
得到
{
返回buttonColor;
}
组
{
buttonColor =价值;
} }
#endregion #区域INotifyPropertyChanged的成员 公共事件PropertyChangedEventHandler的PropertyChanged; 私人无效OnPropertyChanged(字符串propertyName的)
{
如果(的PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的));
}
}
#endregion
}
}
我尽力达成一个记忆卡游戏的目标
检查是否有两证谁是IsCheck / ED属性值= TRUE,并检查有颜色,如果有颜色等于使一个IsCheck / ED财产null,则此切换按钮
但之前algoritam不起作用!
这是FO我试图用谁的根据游戏改结束atoggle按钮才达到一个例子
修改
我怎么能做到这一点使用触发器,当所有的逻辑以上工作NG?...
<控件模板X:键=ButtonControlTemplate1的TargetType ={X:输入切换按钮}>
<网格和GT;
<椭圆NAME =elipse1HEIGHT =65WIDTH =79填充={绑定路径= ButtonColor}能见度=折叠>< /椭圆>
<椭圆NAME =elipse2HEIGHT =65WIDTH =79填充=黑>< /椭圆>
< /网格和GT;
< ControlTemplate.Triggers>
<触发属性=是pressedVALUE =真>
<二传手的TargetName =elipse1属性=能见度VALUE =可见/>
<二传手的TargetName =elipse2属性=能见度VALUE =折叠/>
< /触发> <触发属性=IsCheckVALUE =空>
<二传手的TargetName =elipse1属性=能见度VALUE =折叠/>
<二传手的TargetName =elipse2属性=能见度VALUE =折叠/>
< /触发>
< /ControlTemplate.Triggers>
< /控件模板>
**编辑**
所以summrize我有我的code两个questiones
- 我改变了卡颜色以白色verfy为什么犯规associted给他的切换按钮改变他的颜色呢?
-
我怎么能trugger一个切换按钮,可以在永诺一种颜色?当卡片获得新的颜色,白我要他改变颜色ehite我怎么能实现呢?
非常感谢你。
EDIT2
我唯一的问题是thatbindingto颜色和改变颜色是这样的:
selected.ForEach(电池=> cell.buttonColor = Brushes.White);
犯规使UI注意到它,甚至以为卡更改属性并调用
OnPropertyChanged(ButtonColor);
用户界面并没有改变它
EDIT3
现在我想,我了when我它会给aany切换按钮谁拥有白色的颜色TRIGER白色
<控件模板X:键=ButtonControlTemplate1的TargetType ={X:输入切换按钮}>
<网格和GT;
<椭圆NAME =elipse1HEIGHT =65WIDTH =79填充={绑定路径= ButtonColor}能见度=折叠>< /椭圆>
<椭圆NAME =elipse2HEIGHT =65WIDTH =79填充=黑>< /椭圆>
<椭圆NAME =elipse3HEIGHT =65WIDTH =79填充=黑能见度=坍塌>< /椭圆>
< /网格和GT;
< ControlTemplate.Triggers>
<触发属性=是pressedVALUE =真>
<二传手的TargetName =elipse1属性=能见度VALUE =可见/>
<二传手的TargetName =elipse2属性=能见度VALUE =折叠/>
< /触发> <触发属性=IsCheckVALUE =空>
<二传手的TargetName =elipse1属性=能见度VALUE =折叠/>
<二传手的TargetName =elipse2属性=能见度VALUE =折叠/>
<二传手的TargetName =elipse3属性=能见度VALUE =可见/> < /触发>
当您设置了 ButtonColor
,电话的PropertyChanged
。所以:
#区域colorofbutton 私人刷buttonColor; //使用属性访问,而不是
公共刷ButtonColor
{
得到
{
返回buttonColor;
}
组
{
buttonColor =价值;
OnPropertyChanged(ButtonColor);
} }
#endregion
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>
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
这篇关于记忆卡游戏WPF问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!