【问题标题】:memory card game wpf problem内存卡游戏wpf问题
【发布时间】: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>

** 编辑**

所以总结一下,我对我的代码有两个问题

  1. 我将卡片颜色更改为白色,为什么与他关联的切换按钮也没有改变他的颜色?

  2. 我怎样才能使切换按钮始终为一种颜色?当一张卡片获得新颜色时,我希望他改变颜色以使其成为白色,我该如何实现它?

    非常感谢。

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>

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

    当您设置ButtonColor 时,请致电PropertyChanged。所以:

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

    【讨论】:

    • 这似乎是一个好主意,但它不起作用......当我调试时我看到卡片本身的颜色被引用到按钮更改他的名字但按钮仍然保留一样的颜色
    • 您还需要致电ButtonColor,而不是buttonColor。将该字段设为私有,您将看到在哪里使用它 (cell =&gt; cell.**B**uttonColor = Brushes.White)。我已经编辑了上面的代码来告诉你我的意思;抱歉,我之前没有发现。
    • @Lunivore 它可以工作,但只适用于其中一个而不是 foreach 你能解释一下为什么吗?我怎样才能改变它为他们都工作?。
    • 我不完全确定你的意思,但如果你的意思是两个椭圆都应该是白色的,那么这就是你的问题:Fill="Black" 而不是第二个椭圆上的Fill="{Binding Path=ButtonColor}"。否则,你能解释一下哪些东西不是白色的,应该是白色的吗?
    • 它们都是白色的,我现在的问题是当颜色设置为 ehite 时,我希望这个日食变成白色。你认为什么可以帮助我实现它也许我所有的概念都是错误的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2013-03-27
    • 2013-09-27
    • 1970-01-01
    相关资源
    最近更新 更多