UWP TimePicker MVVM
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 |
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 |
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 '18 at 14:03
Paul StanleyPaul Stanley
642722
642722
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 '18 at 7:38
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 '18 at 23:18
1
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 '18 at 17:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 7:38
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 '18 at 23:18
1
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 '18 at 17:48
add a comment |
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 '18 at 7:38
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 '18 at 23:18
1
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 '18 at 17:48
add a comment |
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 '18 at 23:18
answered Nov 22 '18 at 23:52
DishantDishant
899612
899612
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 '18 at 7:38
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 '18 at 23:18
1
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 '18 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 '18 at 7:38
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 '18 at 23:18
1
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 '18 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 '18 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 '18 at 7:38
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 '18 at 23:18
@PaulStanley Please refer to my edited answer
– Dishant
Nov 25 '18 at 23:18
1
1
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 '18 at 17:48
Thank you for your comprehensive answer.Awesome Sir!
– Paul Stanley
Nov 26 '18 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.
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