UWP TimePicker MVVM
up vote
0
down vote
favorite
I am having problems getting the Time property of the TimePicker into my ViewModel.The method on the ViewModel
TimePickerTimeSelected()
is called but BEFORE the Time property is updated.
private DateTimeOffset _datePickerValue;
public DateTimeOffset datePickerValue
{
get => _datePickerValue;
set => SetProperty(ref _datePickerValue, value);
}
Here isthe XAML
<TimePicker
Name="timePicker"
Time="{x:Bind viewModel.timePickerValue,Mode=TwoWay}"
TimeChanged="{x:Bind viewModel.TimePickerTimeSelected,Mode=TwoWay}">
I want to do some processing with the time when it is selected in the control but the method is called before the timePickerValue is updated.How do I get the new time? ViewModel method:
public void TimePickerTimeSelected()
{
}
mvvm uwp-xaml
add a comment |
up vote
0
down vote
favorite
I am having problems getting the Time property of the TimePicker into my ViewModel.The method on the ViewModel
TimePickerTimeSelected()
is called but BEFORE the Time property is updated.
private DateTimeOffset _datePickerValue;
public DateTimeOffset datePickerValue
{
get => _datePickerValue;
set => SetProperty(ref _datePickerValue, value);
}
Here isthe XAML
<TimePicker
Name="timePicker"
Time="{x:Bind viewModel.timePickerValue,Mode=TwoWay}"
TimeChanged="{x:Bind viewModel.TimePickerTimeSelected,Mode=TwoWay}">
I want to do some processing with the time when it is selected in the control but the method is called before the timePickerValue is updated.How do I get the new time? ViewModel method:
public void TimePickerTimeSelected()
{
}
mvvm uwp-xaml
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am having problems getting the Time property of the TimePicker into my ViewModel.The method on the ViewModel
TimePickerTimeSelected()
is called but BEFORE the Time property is updated.
private DateTimeOffset _datePickerValue;
public DateTimeOffset datePickerValue
{
get => _datePickerValue;
set => SetProperty(ref _datePickerValue, value);
}
Here isthe XAML
<TimePicker
Name="timePicker"
Time="{x:Bind viewModel.timePickerValue,Mode=TwoWay}"
TimeChanged="{x:Bind viewModel.TimePickerTimeSelected,Mode=TwoWay}">
I want to do some processing with the time when it is selected in the control but the method is called before the timePickerValue is updated.How do I get the new time? ViewModel method:
public void TimePickerTimeSelected()
{
}
mvvm uwp-xaml
I am having problems getting the Time property of the TimePicker into my ViewModel.The method on the ViewModel
TimePickerTimeSelected()
is called but BEFORE the Time property is updated.
private DateTimeOffset _datePickerValue;
public DateTimeOffset datePickerValue
{
get => _datePickerValue;
set => SetProperty(ref _datePickerValue, value);
}
Here isthe XAML
<TimePicker
Name="timePicker"
Time="{x:Bind viewModel.timePickerValue,Mode=TwoWay}"
TimeChanged="{x:Bind viewModel.TimePickerTimeSelected,Mode=TwoWay}">
I want to do some processing with the time when it is selected in the control but the method is called before the timePickerValue is updated.How do I get the new time? ViewModel method:
public void TimePickerTimeSelected()
{
}
mvvm uwp-xaml
mvvm uwp-xaml
asked Nov 22 at 14:03
Paul Stanley
608621
608621
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
In your Xaml you are setting Mode=TwoWay
to TimeChanged
Event which is invalid, as TmeChange
is an Event and not a DependencyProperty
.
In order to achieve the desired output please refer to the below code:
Xaml:
<TimePicker
Name="timePicker"
Time="{x:Bind ViewModel.TimePickerValue, Mode=TwoWay}"
TimeChanged="{x:Bind ViewModel.TestTimeChangedEvent}"/>
ViewModel:
private TimeSpan _timePickerValue;
public TimeSpan TimePickerValue
{
get { return _timePickerValue; }
set { _timePickerValue = value;}
}
public void TestTimeChangedEvent(object sender, TimePickerValueChangedEventArgs e)
{
TimePickerValue = e.NewTime;
}
EDIT
As you don't want to use TimePickerValueChangedEventArgs in your ViewModel, you have to create a custom behavior to get the selected time in your VM.
XAML:
<TimePicker
Name="timePicker">
<Interactivity:Interaction.Behaviors>
<behaviors:TimeChangedEventBehavior/>
</Interactivity:Interaction.Behaviors>
</TimePicker>
Behavior:
public sealed class TimeChangedEventBehavior : DependencyObject, IBehavior
{
public DependencyObject AssociatedObject { get; private set; }
public void Attach(DependencyObject associatedObject)
{
if (!(associatedObject is TimePicker tp))
{
throw new ArgumentException("Error Associating Object");
}
this.AssociatedObject = associatedObject;
tp.TimeChanged += OnTimeChanged;
}
private void OnTimeChanged(object sender, TimePickerValueChangedEventArgs e)
{
var timePicker = (sender as TimePicker);
var mainVM = (timePicker.DataContext as MainViewModel);
mainVM.OnTimePickerTimeSelected(timePicker.Time);
}
public void Detach()
{
if (this.AssociatedObject is TimePicker tp)
{
tp.TimeChanged -= this.OnTimeChanged;
}
}
}
ViewModel:
public void OnTimePickerTimeSelected(TimeSpan selectedTime)
{
// Your Logic
}
Yes thanks that does indeed work but your taking a dependency on the UI with the TimePickerVaueChanged which I was hoping to avoid. Is there a way to use MVVM ? You would normally set the property in the viewmodel and that's how you get the value. There would be no reason to have the timePickerVaue otherwise.
– Paul Stanley
Nov 23 at 7:38
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 at 23:18
1
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 at 17:48
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
In your Xaml you are setting Mode=TwoWay
to TimeChanged
Event which is invalid, as TmeChange
is an Event and not a DependencyProperty
.
In order to achieve the desired output please refer to the below code:
Xaml:
<TimePicker
Name="timePicker"
Time="{x:Bind ViewModel.TimePickerValue, Mode=TwoWay}"
TimeChanged="{x:Bind ViewModel.TestTimeChangedEvent}"/>
ViewModel:
private TimeSpan _timePickerValue;
public TimeSpan TimePickerValue
{
get { return _timePickerValue; }
set { _timePickerValue = value;}
}
public void TestTimeChangedEvent(object sender, TimePickerValueChangedEventArgs e)
{
TimePickerValue = e.NewTime;
}
EDIT
As you don't want to use TimePickerValueChangedEventArgs in your ViewModel, you have to create a custom behavior to get the selected time in your VM.
XAML:
<TimePicker
Name="timePicker">
<Interactivity:Interaction.Behaviors>
<behaviors:TimeChangedEventBehavior/>
</Interactivity:Interaction.Behaviors>
</TimePicker>
Behavior:
public sealed class TimeChangedEventBehavior : DependencyObject, IBehavior
{
public DependencyObject AssociatedObject { get; private set; }
public void Attach(DependencyObject associatedObject)
{
if (!(associatedObject is TimePicker tp))
{
throw new ArgumentException("Error Associating Object");
}
this.AssociatedObject = associatedObject;
tp.TimeChanged += OnTimeChanged;
}
private void OnTimeChanged(object sender, TimePickerValueChangedEventArgs e)
{
var timePicker = (sender as TimePicker);
var mainVM = (timePicker.DataContext as MainViewModel);
mainVM.OnTimePickerTimeSelected(timePicker.Time);
}
public void Detach()
{
if (this.AssociatedObject is TimePicker tp)
{
tp.TimeChanged -= this.OnTimeChanged;
}
}
}
ViewModel:
public void OnTimePickerTimeSelected(TimeSpan selectedTime)
{
// Your Logic
}
Yes thanks that does indeed work but your taking a dependency on the UI with the TimePickerVaueChanged which I was hoping to avoid. Is there a way to use MVVM ? You would normally set the property in the viewmodel and that's how you get the value. There would be no reason to have the timePickerVaue otherwise.
– Paul Stanley
Nov 23 at 7:38
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 at 23:18
1
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 at 17:48
add a comment |
up vote
1
down vote
accepted
In your Xaml you are setting Mode=TwoWay
to TimeChanged
Event which is invalid, as TmeChange
is an Event and not a DependencyProperty
.
In order to achieve the desired output please refer to the below code:
Xaml:
<TimePicker
Name="timePicker"
Time="{x:Bind ViewModel.TimePickerValue, Mode=TwoWay}"
TimeChanged="{x:Bind ViewModel.TestTimeChangedEvent}"/>
ViewModel:
private TimeSpan _timePickerValue;
public TimeSpan TimePickerValue
{
get { return _timePickerValue; }
set { _timePickerValue = value;}
}
public void TestTimeChangedEvent(object sender, TimePickerValueChangedEventArgs e)
{
TimePickerValue = e.NewTime;
}
EDIT
As you don't want to use TimePickerValueChangedEventArgs in your ViewModel, you have to create a custom behavior to get the selected time in your VM.
XAML:
<TimePicker
Name="timePicker">
<Interactivity:Interaction.Behaviors>
<behaviors:TimeChangedEventBehavior/>
</Interactivity:Interaction.Behaviors>
</TimePicker>
Behavior:
public sealed class TimeChangedEventBehavior : DependencyObject, IBehavior
{
public DependencyObject AssociatedObject { get; private set; }
public void Attach(DependencyObject associatedObject)
{
if (!(associatedObject is TimePicker tp))
{
throw new ArgumentException("Error Associating Object");
}
this.AssociatedObject = associatedObject;
tp.TimeChanged += OnTimeChanged;
}
private void OnTimeChanged(object sender, TimePickerValueChangedEventArgs e)
{
var timePicker = (sender as TimePicker);
var mainVM = (timePicker.DataContext as MainViewModel);
mainVM.OnTimePickerTimeSelected(timePicker.Time);
}
public void Detach()
{
if (this.AssociatedObject is TimePicker tp)
{
tp.TimeChanged -= this.OnTimeChanged;
}
}
}
ViewModel:
public void OnTimePickerTimeSelected(TimeSpan selectedTime)
{
// Your Logic
}
Yes thanks that does indeed work but your taking a dependency on the UI with the TimePickerVaueChanged which I was hoping to avoid. Is there a way to use MVVM ? You would normally set the property in the viewmodel and that's how you get the value. There would be no reason to have the timePickerVaue otherwise.
– Paul Stanley
Nov 23 at 7:38
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 at 23:18
1
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 at 17:48
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
In your Xaml you are setting Mode=TwoWay
to TimeChanged
Event which is invalid, as TmeChange
is an Event and not a DependencyProperty
.
In order to achieve the desired output please refer to the below code:
Xaml:
<TimePicker
Name="timePicker"
Time="{x:Bind ViewModel.TimePickerValue, Mode=TwoWay}"
TimeChanged="{x:Bind ViewModel.TestTimeChangedEvent}"/>
ViewModel:
private TimeSpan _timePickerValue;
public TimeSpan TimePickerValue
{
get { return _timePickerValue; }
set { _timePickerValue = value;}
}
public void TestTimeChangedEvent(object sender, TimePickerValueChangedEventArgs e)
{
TimePickerValue = e.NewTime;
}
EDIT
As you don't want to use TimePickerValueChangedEventArgs in your ViewModel, you have to create a custom behavior to get the selected time in your VM.
XAML:
<TimePicker
Name="timePicker">
<Interactivity:Interaction.Behaviors>
<behaviors:TimeChangedEventBehavior/>
</Interactivity:Interaction.Behaviors>
</TimePicker>
Behavior:
public sealed class TimeChangedEventBehavior : DependencyObject, IBehavior
{
public DependencyObject AssociatedObject { get; private set; }
public void Attach(DependencyObject associatedObject)
{
if (!(associatedObject is TimePicker tp))
{
throw new ArgumentException("Error Associating Object");
}
this.AssociatedObject = associatedObject;
tp.TimeChanged += OnTimeChanged;
}
private void OnTimeChanged(object sender, TimePickerValueChangedEventArgs e)
{
var timePicker = (sender as TimePicker);
var mainVM = (timePicker.DataContext as MainViewModel);
mainVM.OnTimePickerTimeSelected(timePicker.Time);
}
public void Detach()
{
if (this.AssociatedObject is TimePicker tp)
{
tp.TimeChanged -= this.OnTimeChanged;
}
}
}
ViewModel:
public void OnTimePickerTimeSelected(TimeSpan selectedTime)
{
// Your Logic
}
In your Xaml you are setting Mode=TwoWay
to TimeChanged
Event which is invalid, as TmeChange
is an Event and not a DependencyProperty
.
In order to achieve the desired output please refer to the below code:
Xaml:
<TimePicker
Name="timePicker"
Time="{x:Bind ViewModel.TimePickerValue, Mode=TwoWay}"
TimeChanged="{x:Bind ViewModel.TestTimeChangedEvent}"/>
ViewModel:
private TimeSpan _timePickerValue;
public TimeSpan TimePickerValue
{
get { return _timePickerValue; }
set { _timePickerValue = value;}
}
public void TestTimeChangedEvent(object sender, TimePickerValueChangedEventArgs e)
{
TimePickerValue = e.NewTime;
}
EDIT
As you don't want to use TimePickerValueChangedEventArgs in your ViewModel, you have to create a custom behavior to get the selected time in your VM.
XAML:
<TimePicker
Name="timePicker">
<Interactivity:Interaction.Behaviors>
<behaviors:TimeChangedEventBehavior/>
</Interactivity:Interaction.Behaviors>
</TimePicker>
Behavior:
public sealed class TimeChangedEventBehavior : DependencyObject, IBehavior
{
public DependencyObject AssociatedObject { get; private set; }
public void Attach(DependencyObject associatedObject)
{
if (!(associatedObject is TimePicker tp))
{
throw new ArgumentException("Error Associating Object");
}
this.AssociatedObject = associatedObject;
tp.TimeChanged += OnTimeChanged;
}
private void OnTimeChanged(object sender, TimePickerValueChangedEventArgs e)
{
var timePicker = (sender as TimePicker);
var mainVM = (timePicker.DataContext as MainViewModel);
mainVM.OnTimePickerTimeSelected(timePicker.Time);
}
public void Detach()
{
if (this.AssociatedObject is TimePicker tp)
{
tp.TimeChanged -= this.OnTimeChanged;
}
}
}
ViewModel:
public void OnTimePickerTimeSelected(TimeSpan selectedTime)
{
// Your Logic
}
edited Nov 25 at 23:18
answered Nov 22 at 23:52
Dishant
832511
832511
Yes thanks that does indeed work but your taking a dependency on the UI with the TimePickerVaueChanged which I was hoping to avoid. Is there a way to use MVVM ? You would normally set the property in the viewmodel and that's how you get the value. There would be no reason to have the timePickerVaue otherwise.
– Paul Stanley
Nov 23 at 7:38
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 at 23:18
1
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 at 17:48
add a comment |
Yes thanks that does indeed work but your taking a dependency on the UI with the TimePickerVaueChanged which I was hoping to avoid. Is there a way to use MVVM ? You would normally set the property in the viewmodel and that's how you get the value. There would be no reason to have the timePickerVaue otherwise.
– Paul Stanley
Nov 23 at 7:38
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 at 23:18
1
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 at 17:48
Yes thanks that does indeed work but your taking a dependency on the UI with the TimePickerVaueChanged which I was hoping to avoid. Is there a way to use MVVM ? You would normally set the property in the viewmodel and that's how you get the value. There would be no reason to have the timePickerVaue otherwise.
– Paul Stanley
Nov 23 at 7:38
Yes thanks that does indeed work but your taking a dependency on the UI with the TimePickerVaueChanged which I was hoping to avoid. Is there a way to use MVVM ? You would normally set the property in the viewmodel and that's how you get the value. There would be no reason to have the timePickerVaue otherwise.
– Paul Stanley
Nov 23 at 7:38
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 at 23:18
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 at 23:18
1
1
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 at 17:48
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 at 17:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432685%2fuwp-timepicker-mvvm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown