代码之家  ›  专栏  ›  技术社区  ›  yoav.str

存储卡游戏WPF问题

  •  2
  • yoav.str  · 技术社区  · 14 年前

    我有一个记忆卡游戏,我的绑定是到

    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;
    
            }
    

    如果(已选择.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
    }
    

    }

    我努力实现记忆卡游戏的目标 检查是否有两张卡的is check/ed属性值为true,如果颜色相等,则检查其中的颜色。在is check/ed属性中,使此切换按钮为空。 但是先前的算法不起作用!!

    这是一个例子,我试图通过结束一个切换按钮来实现什么,这个按钮根据游戏的不同而改变。 编辑 当上面所有的逻辑都在工作时,我如何使用触发器来完成它?…

    <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>
    

    **编辑**

    总结一下,我有两个关于我的代码的问题

    1. 我把卡片的颜色改成了白色,为什么连拨动按钮也不改颜色呢?
    2. 我怎样才能将切换按钮调整为一种颜色?当一张牌变成新颜色时,我要他把颜色改成白色,我怎么才能做到?

      非常感谢。

    编辑2

    我唯一的问题是绑定到颜色并像这样更改颜色:

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

    不要让用户界面注意到它,甚至认为卡属性发生了变化并调用

      OnPropertyChanged("ButtonColor");
    

    用户界面不会改变它

    编辑3

    现在我想要一个触发器,当我在它上面时,它会给任何有白色的切换按钮白色

           <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 回复  |  直到 14 年前
        1
  •  1
  •   Lunivore    14 年前

    当你设置你的 ButtonColor ,呼叫 PropertyChanged . 所以:

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