0

如何在 xaml 数据透视页面的用户控件上的文本块中绑定计时器滴答计数。我曾经从类中获取绑定值,更改的属性不起作用,它返回空值。

public class sample
{
int timercount = 0;
public int Timercount
{
    get
    {
        return timercount;
    }
    set
    { 
        timercount = value;
        base.OnPropertyChanged("Timercount");
    }
 }
}

public class ViewModelBaseEx : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
     PropertyChangedEventHandler handler = PropertyChanged;
     if (handler != null)
     {
        handler(this, new PropertyChangedEventArgs(name));
     }
}

}

xml:

<TextBlock Height="34" Name="txtTimer" Width="57" TextAlignment="Left" FontSize="20" TextWrapping="Wrap" Text="{Binding Timercount,Mode=TwoWay}" VerticalAlignment="Top" />

这是我在 xaml 页面上设置的绑定

            TimerPageModel mainPage = new TimerPageModel();

            Binding binding = new Binding("Timercount");
            binding.Source = mainPage;
            binding.Mode = BindingMode.TwoWay;
            TextBlock tblk = qplayer.txtTimer;
            BindingOperations.SetBinding(tblk, TextBlock.TextProperty, binding);

qplayer 是一个用户控件,TimerPageModel 是一个继承 ViewModelBaseEx PropertyChanged 的​​视图模型正在工作,但用户控件文本块中没有任何绑定

绑定适用于计时器滴答声

    public class TimerPageModel : ViewModelBaseEx
    {
        private DispatcherTimer timer = new DispatcherTimer();
        private Random random = new Random();
        static int tmrRnd = 30;
        public TimerPageModel()
        {
            timer.Interval = new TimeSpan(0, 0, 5);
            timer.Tick += OnTimer;
            timer.Start();
        }
    }
        int timercount = 0;
        public int Timercount
        {
            get
            {
                return timercount;
            }
            set
            {
                timercount = value;
                base.OnPropertyChanged("Timercount");
            }
        }
        void OnTimer(object sender, EventArgs eventArguments)
        {
            //CurrentValue = random.Next(100);

            Timercount = tmrRnd--;
        }
4

0 回答 0