【发布时间】:2023-03-28 20:43:02
【问题描述】:
我有一个记忆卡游戏,我的绑定是
public ObservableCollection<List<memoryCard>> MyCollection { get; set; }//holding the array
在哪里初始化是这样的:
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 属性 value = true 并检查颜色是否颜色相等 使 IsCheck/ed 属性中的此切换按钮为 null 但是之前的算法不起作用!!
这是我试图通过根据游戏更改的结束切换按钮来实现的示例 编辑 当上述所有逻辑都正常工作时,我如何使用触发器来做到这一点?...
<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);
没有让用户界面注意到它,即使卡片属性发生了变化并调用
OnPropertyChanged("ButtonColor");
用户界面不会改变它
EDIT3
现在我想要一个触发器,当我打开它时,它会给任何白色的切换按钮提供白色
<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>
【问题讨论】: