开发者

get updated class timers time in code behind

I have a MVC class where i am inheriting INotifyPropertyChanged

I have a clock timer where for minute second and hour change the coontrol in xaml is updated with OnPropertyChanged("Time"); for example. this workes great for an analog clock in rotating its 3 hands. However, i want to make a digital clock by drawing its lines or toggle visibility in rectangle 7 segments.

so, in order to do that i have to do it in the code behind. Therefore, i need to be able to access that time that i created in the ClockViewModel : INotifyPropertyChanged class so, from the code behind i want to use that time to toggle visibility to a rectangle or paint segments to display time.

I have an mvc analog clock that rotates hands from onproperty change but i dont see how to t开发者_如何学编程oggle visibility the same way or draw / paint.

so, is there a way to get the time from the class in the code behind? and or get the on property change when the time changes? or do i have to make a separate non MVC timer in the code behind?

here is the class

class ClockViewModel : INotifyPropertyChanged
{




        public ClockViewModel()
        {
            _timer = new System.Timers.Timer(1000);
            _timer.Elapsed += _timer_Elapsed;
            _timer.Start();
        }

what i want to do is get the constant time from the class run a switch statement on it so i can toggle visibility on some lines to show the time. Or, just draw the appropriate lines accordingly.

I can bind a textblock text to OnPropertyChanged("DigitalTime"); to display the time like digital however that defeats what i want to do.


To do this I would add properties to my view model that defined the visibility for each line segment.

Then each property getter would calculate whether the line should be visible based on the number to display.

Now you could bind the Visibility property of each line to the corresponding property in your view model and call OnPropertyChanged("") (with an empty string) to raise all listeners for your view model.

An easy improvement would be to factor out a user control that displayed one digit in your digital clock. Then you could reuse that control for each digit you needed to display.

Edit:
I won't code the entire control for you (what is the use of that?), but let me make an example. I will assume that you have a set of line controls, maybe in a grid or something like that. To draw a number you need to show some lines and hide others.
Now you need some xaml and a view model that supplies the data.

Your Xaml should look something like this:

<Line X1="10" X2="10" Y1="10" Y2="20" Visibility="{Binding Line1Visibility, Mode=OneWay}" />
<Line X1="10" X2="20" Y2="10" Y2="10" Visibility="{Binding Line2Visibility, Mode=OneWay}" />
.... and so on

You need to define all the lines, but you don't need to name them. Don't use my X and Y values. They are for the example only. Also note the binding for the Visibilily. It is one-way since we don't want to set it from our control.

Now you need your view model. It should define properties for all the visibilities and a property where you set the number to display. It should be something like this:

public class DigitalNumberViewModel : INotifyPropertyChanged
{
  private int _numberToDisplay;
  public int NumberToDisplay
  { 
    get { return _numberToDisplay; }
    set 
    {
        _numberToDisplay = value;
        OnPropertyChanged("");
    }
  }

  public Visibility Line1Visibility 
  {
    get 
    {
      switch (NumberToDisplay)
      {
        case 0:
          return Visibility.Visible;
        case 1:
          return Visibility.Hidden;
        //... and so on
      }
    }
  }

  public Visibility Line2Visibility
  { .... }

  // ... and so on

}

Now you have a view model that defines the visibility for all the lines and a property that you can set that will call the property changed event for all properties in your view model. You should be able to fill in the rest yourself. :-)

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜