2010-10-06 88 views
2

我有一个memorey纸牌游戏,我的结合是记忆卡游戏WPF问题

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

其中initlized这样的:(!selected.Count = 0)

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; 

     } 

如果选中。 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属性中设置这个切换按钮为空 但前面的算法不起作用!

这是FO我试图用谁的根据游戏 编辑改结束atoggle按钮才达到一个例子 我怎么能做到这一点使用触发器时,所有上述逻辑正在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> 

**编辑**

所以summrize我有我的两个代码questiones

  1. 我改变了卡的颜色为白色的verfy为什么与他相关联的切换按钮也改变了他的颜色?
  2. 我怎样才能让一个切换按钮变成一种颜色?当一张卡获得新的颜色时,白色我希望他将他的颜色改为白色,我该如何实现它?

    非常感谢。

EDIT2

我唯一的问题是thatbindingto颜色和改变颜色像这样:

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

犯规使UI注意到它,甚至以为卡属性更改和呼叫

OnPropertyChanged("ButtonColor"); 

界面不变它

EDIT3

现在我想那了when我是在它会给aany切换按钮谁拥有白色白色

 <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

当您设置一个TRIGER你ButtonColor,调用PropertyChanged 。所以:

#region colorofbutton 

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

    } 
    #endregion 
+0

它似乎是一个好主意,但它不工作...卡片上的颜色自己当我调试时,我发现卡片上的Woh属于按钮更改他的名字,但该按钮仍然具有相同的颜色 – 2010-10-06 12:55:47

+0

您还需要调用'ButtonColor',而不是'buttonColor'。将该字段设置为私人,你会看到你使用它的地方('cell => cell。** B ** uttonColor = Brushes.White')。我编辑了上面的代码来向你展示我的意思;对不起,我以前没有发现。 – Lunivore 2010-10-06 13:21:00

+0

@Lunivore它的作品,但只是其中一个,而不是foreach你能解释我为什么?我怎么能改变它为他们工作? – 2010-10-06 15:33:23