2010-10-06 87 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