I was wondering if it is possible to bind Slider
's Value
to DispatcherTimer
's Interval
? I know I can subscribe for ValueChanged
event and then just simply do timer.Interval = new TimeSpan.FromMilliseconds(slider.Value)
, but is it possible to achieve the same effect using just Binding
mechanism? Thanks in advance :)
Asked
Active
Viewed 1,136 times
0

monkog
- 15
- 2
- 10
-
You need a `IValueConverter` in between binding because Interval is if type TimeSpan and Value is of type double. Have you tried it? – Rohit Vats Aug 07 '14 at 14:00
-
Yes, I know about converters, but I'm having trouble strictly with binding these values. `Interval` has no `DependencyProperty`, but there has to be a way to do it :) – monkog Aug 07 '14 at 22:28
1 Answers
0
You can't data bind between a DispatcherTimer.Interval
property and a Slider.Value
property directly. What you can do is to declare a property that you can data bind to the Slider.Value
property and in the setter
, set your DispatcherTimer.Interval
property. Try something like this::
public double Milliseconds
{
get { return milliseconds; }
set
{
milliseconds = value;
NotifyPropertyChanged("Milliseconds");
yourTimer.Interval(TimeSpan.FromMilliseconds((int)milliseconds);
}
}
...
<Slider Value="{Binding Milliseconds}" ... />

Sheridan
- 68,826
- 24
- 143
- 183
-
Thank you @Sheridan for your response. It's almost what I was looking for, but I guess I'm missing something. When I use `NotifyPropertyChanged("Milliseconds");` I keep getting an error `"The name does not exist in current context"`. I checked and this function is defined for `DataGrid`. What should I use instead? Or mabe defining a separate class with `DispatcherTimer` inside that implements `INotifyPropertyChanged` will be the answer? – monkog Aug 07 '14 at 22:30
-
I assumed that you were implementing the `INotifyPropertyChanged` interface in your class (like you should be), but if not, just omit that line. – Sheridan Aug 08 '14 at 07:47
-
I should have posted some code. I currently have my `DispatcherTimer` in my `MainWindow` class. So just to be sure, Is it better to create some `TimerHelper` class which implements `INotifyPropertyChanged` and use it's instance instead of `DispatcherTimer` in `MainWindow`? – monkog Aug 08 '14 at 08:15
-
It is customary to define a class that contains *all data properties and functionality* that is required by the `Window` and set an instance of that as the `Window.DataContext`. Your `Milliseconds` property and timer should go in there. – Sheridan Aug 08 '14 at 08:33