从静态对象更新WPF标签

时间:2014-07-28 03:34:56

标签: c# .net wpf

我一直在努力让这一整天工作。我正在尝试学习C#并编写一个二十一点程序。我以前从未使用过WPF,而且它的一切都非常令人困惑和令人沮丧。

我的窗口上有三个标签,我试图从viewmodel类的静态实例(BlackjackViewModel)更新:

<Label x:Name="lblPlayerTotal" Content="{Binding Source={x:Static classes:BlackjackViewModel.Instance}, Path=PlayerTotal, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="10,264,0,0" VerticalAlignment="Top" Width="38"/>
<Label x:Name="lblDealerTotal" Content="{Binding Source={x:Static classes:BlackjackViewModel.Instance}, Path=DealerTotal, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="10,160,0,0" VerticalAlignment="Top"/>
<Label x:Name="lblCardsRemaining" Content="{Binding Source={x:Static classes:BlackjackViewModel.Instance}, Path=CardsRemaining, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="584,10,0,0" VerticalAlignment="Top"/>

我无法显示任何内容,标签只是空白。根据调试器BlackjackViewModel正在发送PropertyChanged事件,但据我所知,没有什么是在听。 viewmodel的属性都如下所示:

    public int DealerTotal {
        get { return dealerTotal; } 
        set 
        {
            dealerTotal = value;
            OnPropertyChanged("DealerTotal");    
        }
    }

并且在视图模型更新时触发OnPropertyChanged上的断点,以便发送事件。

我尝试重构我的代码,使BlackjackViewModel实例不是静态的,但也没有帮助。这很困难,因为我有一个EventListener设置,当卡片从牌组发出时更新了viewmodel的剩余牌数,所以我不得不将其评论出来。它仍然无法使用以下XAML:

    <Label x:Name="lblPlayerTotal" DataContext="blackjackViewModel" Content="{Binding PlayerTotal}" HorizontalAlignment="Left" Margin="10,264,0,0" VerticalAlignment="Top" Width="38"/>
    <Label x:Name="lblDealerTotal" DataContext="blackjackViewModel" Content="{Binding DealerTotal}" HorizontalAlignment="Left" Margin="10,160,0,0" VerticalAlignment="Top"/>
    <Label x:Name="lblCardsRemaining" DataContext="blackjackViewModel" Content="{Binding CardsRemaining}" HorizontalAlignment="Left" Margin="584,10,0,0" VerticalAlignment="Top"/>

如何在viewmodel实例更新时实际更新标签?

更新:使用更正的静态单件设置,标签包含数字0,而不是空白。这是ViewModel。更新属性时,调试器会在OnPropertyChanged中设置的断点处中断。我在输出视图中没有出现绑定错误。我将下载一个WPF示踪剂。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

namespace BlackjackGame
{
    class BlackjackViewModel : INotifyPropertyChanged
    {
        private static BlackjackViewModel __instance = null;

        private int dealerTotal;
        private int playerTotal;
        private int cardsRemaining;

        public event PropertyChangedEventHandler PropertyChanged;

        static BlackjackViewModel()
        {
            __instance = new BlackjackViewModel();
        }

        public int CardsRemaining
        {
            get { return cardsRemaining; }
            set
            {
                cardsRemaining = value;
                OnPropertyChanged("CardsRemaining");
            }
        }
        public int PlayerTotal
        {
            get { return playerTotal; }
            set
            {
                playerTotal = value;
                OnPropertyChanged("PlayerTotal");
            }
        }
        public int DealerTotal {
            get { return dealerTotal; } 
            set 
            {
                dealerTotal = value;
                OnPropertyChanged("DealerTotal");    
            }
        }

        private void OnPropertyChanged(string propertyName)
        {      
           if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public static BlackjackViewModel Instance
        {
            get { return __instance; }
        }

    }
}

0 个答案:

没有答案