Click here to Skip to main content
15,915,750 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
namespace WpfApplication3
{
    public class UpdatableDateTime : INotifyPropertyChanged
    {
        public DateTime Now
        {
            get
            {
                return DateTime.Now;
            }
        }

        private DispatcherTimer tmrDoUpdate;
        
        public UpdatableDateTime()
        {
            tmrDoUpdate = new DispatcherTimer();
            tmrDoUpdate.Interval = TimeSpan.FromSeconds(1);
            tmrDoUpdate.Tick += new EventHandler(tmrDoUpdate_Tick);
            tmrDoUpdate.Start();
        }

        void tmrDoUpdate_Tick(object sender, EventArgs e)
        {
            OnPropertyChanged(new PropertyChangedEventArgs("Now"));
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }

        #endregion
  }
}

This will be for a clock i am creating in wpf.
I already got seconds but i still need to convert it in to angle.

The syntax below dont work when I include "*6" which is the conversion of seconds to angle so I have to convert it inside the class

<rotatetransform angle="{Binding Source={StaticResource updatableDateTime*6}, Path=ConvertMinute}"></rotatetransform>
Posted
Updated 29-Aug-10 4:02am
v4

Firstly, the class you have above looks correct, there is no need to add an angle to it in case you were thinking of this.

Secondly, Create a converter class (inheriting from IValueConverter) that takes the time and returns the angle from the given number of seconds (Number of Seconds * 6). Include the converter's namespace in the XAML, then pass the converter into the binding:
{Binding {StaticResource {StaticResource updatableDateTime}, Converter={StaticResource YourConverterName}}


Here is an article about converters:
Value Converter in WPF[^]
 
Share this answer
 
Comments
gherard 29-Aug-10 23:25pm    
thanks keith!

IValueConverter is one way to do it.

[ValueConversion(typeof(int), typeof(double))]
public class ClockAngleConverter : IValueConverter
{


public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((int)value * 6);
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
Keith Barrow 1-Sep-10 4:19am    
Thanks. I think Kubajzz's answer also gives another possible answer to the solution. I say this with a *slight* caveat is that it is undesirable to *directly* change your base object model to suit your UI. There is a well-established UI pattern Model-View-ViewModel, which is well worth looking at if your are going to do lots of WPF work. Your UpdatableDateTime class would be the ViewModel (DateTime would be the model in this case) seen this way Kubajzz's answer is also valid, but (for example) it ties then model down to an analogue clock, whereas with a converter you could re-skin to digital for example.
One way to do it is to use a converter like Keith Barrow suggested. Another way would be to change your data model - add a property called Angle, do all necessary calculations in code and bind to the new property.

The second method is simpler and faster to implement, but it's up to you to choose which solution fits your needs...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900