如何在xaml pivot页面上绑定usercontrol上textblock中的计时器滴答计数。 我曾经从类中获取绑定值,属性更改不起作用,它返回null值。
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));
}
}
}
xaml:
<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的viewmodel PropertyChanged正在工作,但在usercontrol文本块
中没有绑定任何内容绑定适用于计时器滴答
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--;
}