x:bind UI not update when PropertyChanged in UWPWhy is it important to override GetHashCode when Equals method is overridden?When to use struct?How do I update the GUI from another thread?How to get property name from the sender object of an INotifyPropertyChanged PropertyChanged eventDatabinding issue with stopwatched elapsedConnecting SQL Azure to Windows Phone 7 using WCFEntity Framework 5 Updating a RecordWPF Binding not updating from DispatcherTimerObservable.Interval not updating UI with expected frequencyUWP Xaml Textblock Data Binding - UI not updating even though the property is updated
Analog is Obtuse!
How hard is it to sell a home which is currently mortgaged?
Coefficients of the characteristic polynomial
can’t run a function against EXEC
When to apply Lorentz transformations and laws of time dilations and length contractions: explanations
Three column layout
“Faire” being used to mean “avoir l’air”?
Math PhD in US vs Master + PhD in Europe
How to start learning the piano again
What's the point of DHS warning passengers about Manila airport?
Was touching your nose a greeting in second millenium Mesopotamia?
Was "I have the farts, again" broadcast from the Moon to the whole world?
For people who believe in Jesus and not the devil, what happend in the desert?
Do I have to roll to maintain concentration if a target other than me who is affected by my concentration spell takes damage?
Why did this meteor appear cyan?
How to determine what is the correct level of detail when modelling?
Can you sign using a digital signature itself?
How was film developed in the late 1920s?
The difference between Rad1 and Rfd1
Should I hide continue button until tasks are completed?
Why won't the ground take my seed?
I played my first (rapid) tournament recently and I wanted to calculate my ELO
In native German words, is Q always followed by U, as in English?
Bash echo $-1 prints hb1. Why?
x:bind UI not update when PropertyChanged in UWP
Why is it important to override GetHashCode when Equals method is overridden?When to use struct?How do I update the GUI from another thread?How to get property name from the sender object of an INotifyPropertyChanged PropertyChanged eventDatabinding issue with stopwatched elapsedConnecting SQL Azure to Windows Phone 7 using WCFEntity Framework 5 Updating a RecordWPF Binding not updating from DispatcherTimerObservable.Interval not updating UI with expected frequencyUWP Xaml Textblock Data Binding - UI not updating even though the property is updated
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm using x:Bind
and INotifyPropertyChanged
to update UI in UWP application. But it behaves like OneTime
binding even though I set it to OneWay
.
Bindings.Update()
works, but I want to know why INotifyPropertyChanged
fails.
XAML
<TextBlock Text="x:Bind staffVM.Name, Mode=OneWay"/>
Code-behind:
private StaffViewModel staffVM;
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
// I want to change staffVM according to ListView's selection.
staffVM = staffListView.SelectedItem as StaffViewModel;
staffVM.Update(); // If change this to Bindings.Update(), It works.
ViewModel:
public class StaffViewModel: INotifyPropertyChanged
private Character character;
public string Name => character.name == string.Empty ? null : character.name;
public void Update()
RaisePropertyChanged(string.Empty);
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName]string propName = null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
c# mvvm uwp
add a comment |
I'm using x:Bind
and INotifyPropertyChanged
to update UI in UWP application. But it behaves like OneTime
binding even though I set it to OneWay
.
Bindings.Update()
works, but I want to know why INotifyPropertyChanged
fails.
XAML
<TextBlock Text="x:Bind staffVM.Name, Mode=OneWay"/>
Code-behind:
private StaffViewModel staffVM;
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
// I want to change staffVM according to ListView's selection.
staffVM = staffListView.SelectedItem as StaffViewModel;
staffVM.Update(); // If change this to Bindings.Update(), It works.
ViewModel:
public class StaffViewModel: INotifyPropertyChanged
private Character character;
public string Name => character.name == string.Empty ? null : character.name;
public void Update()
RaisePropertyChanged(string.Empty);
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName]string propName = null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
c# mvvm uwp
add a comment |
I'm using x:Bind
and INotifyPropertyChanged
to update UI in UWP application. But it behaves like OneTime
binding even though I set it to OneWay
.
Bindings.Update()
works, but I want to know why INotifyPropertyChanged
fails.
XAML
<TextBlock Text="x:Bind staffVM.Name, Mode=OneWay"/>
Code-behind:
private StaffViewModel staffVM;
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
// I want to change staffVM according to ListView's selection.
staffVM = staffListView.SelectedItem as StaffViewModel;
staffVM.Update(); // If change this to Bindings.Update(), It works.
ViewModel:
public class StaffViewModel: INotifyPropertyChanged
private Character character;
public string Name => character.name == string.Empty ? null : character.name;
public void Update()
RaisePropertyChanged(string.Empty);
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName]string propName = null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
c# mvvm uwp
I'm using x:Bind
and INotifyPropertyChanged
to update UI in UWP application. But it behaves like OneTime
binding even though I set it to OneWay
.
Bindings.Update()
works, but I want to know why INotifyPropertyChanged
fails.
XAML
<TextBlock Text="x:Bind staffVM.Name, Mode=OneWay"/>
Code-behind:
private StaffViewModel staffVM;
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
// I want to change staffVM according to ListView's selection.
staffVM = staffListView.SelectedItem as StaffViewModel;
staffVM.Update(); // If change this to Bindings.Update(), It works.
ViewModel:
public class StaffViewModel: INotifyPropertyChanged
private Character character;
public string Name => character.name == string.Empty ? null : character.name;
public void Update()
RaisePropertyChanged(string.Empty);
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName]string propName = null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
c# mvvm uwp
c# mvvm uwp
edited Mar 25 at 16:15
Martin Zikmund
27.2k6 gold badges42 silver badges65 bronze badges
27.2k6 gold badges42 silver badges65 bronze badges
asked Mar 25 at 12:11
KirizKiriz
851 silver badge8 bronze badges
851 silver badge8 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Firstly, you need to specify the name of the variable that you want to update:
public void Update()
RaisePropertyChanged(nameof(Name));
Documentation and sample: https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.data.inotifypropertychanged.propertychanged
Secondly, by default x:Bind is OneTime
To fix it, add Mode="OneWay"
Mode Specifies the binding mode, as one of these strings: "OneTime", "OneWay", or "TwoWay". The default is "OneTime". Note that this differs from the default for Binding, which is "OneWay" in most cases.
Please read documentation
https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/x-bind-markup-extension
1.The PropertyChanged event can indicate that all properties on the object have changed by using String.Empty for the PropertyName property of the PropertyChangedEventArgs.
according to your documentation in first part. 2. I've already setMode="OneWay"
– Kiriz
Mar 25 at 13:41
Ok, next question is what are you expect? I cannot see in your code any changes that affect UI. Just Update method where you do nothing just invoke Property changed. But none of the properties changed. You need to change property and only after that invoke property changed event
– Yauhen Sampir
Mar 26 at 6:55
add a comment |
The problem here is not on the level of the StaffViewModel
class, but on the level of the page. When you do:
staffVM = staffListView.SelectedItem as StaffViewModel;
The UI has no notification about the fact that the staffVM
field has changed. So the binding is still pointing to the old instance of StaffViewModel
. Hence when you do staffVM.Update()
, it does notify about changes, but the UI is not listening to that instance - it is still listening to notifications on the first selected item. Bindings.Update()
fixes this because it completely re-evaluates all bindings so it will "get" the new value of staffVM
field.
Solution would be to implement INotifyPropertyChanged
on the Page
and encapsulate the staffVM
in a property which raises PropertyChanged
event.
Ideally I would however suggest creating a "root" view model, which you will set only once and will not change and which will contain the selected item as its property. This way you don't have to implement INotifyPropertyChanged
in the Page
and its code-behind will be simpler. As a result you will have something like the following in the code-behind:
public RootViewModel VM get; = new RootViewModel();
And in XAML:
<TextBlock Text="x:Bind VM.SelectedStaff.Name, Mode=OneWay"/>
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%2f55337531%2fxbind-ui-not-update-when-propertychanged-in-uwp%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Firstly, you need to specify the name of the variable that you want to update:
public void Update()
RaisePropertyChanged(nameof(Name));
Documentation and sample: https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.data.inotifypropertychanged.propertychanged
Secondly, by default x:Bind is OneTime
To fix it, add Mode="OneWay"
Mode Specifies the binding mode, as one of these strings: "OneTime", "OneWay", or "TwoWay". The default is "OneTime". Note that this differs from the default for Binding, which is "OneWay" in most cases.
Please read documentation
https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/x-bind-markup-extension
1.The PropertyChanged event can indicate that all properties on the object have changed by using String.Empty for the PropertyName property of the PropertyChangedEventArgs.
according to your documentation in first part. 2. I've already setMode="OneWay"
– Kiriz
Mar 25 at 13:41
Ok, next question is what are you expect? I cannot see in your code any changes that affect UI. Just Update method where you do nothing just invoke Property changed. But none of the properties changed. You need to change property and only after that invoke property changed event
– Yauhen Sampir
Mar 26 at 6:55
add a comment |
Firstly, you need to specify the name of the variable that you want to update:
public void Update()
RaisePropertyChanged(nameof(Name));
Documentation and sample: https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.data.inotifypropertychanged.propertychanged
Secondly, by default x:Bind is OneTime
To fix it, add Mode="OneWay"
Mode Specifies the binding mode, as one of these strings: "OneTime", "OneWay", or "TwoWay". The default is "OneTime". Note that this differs from the default for Binding, which is "OneWay" in most cases.
Please read documentation
https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/x-bind-markup-extension
1.The PropertyChanged event can indicate that all properties on the object have changed by using String.Empty for the PropertyName property of the PropertyChangedEventArgs.
according to your documentation in first part. 2. I've already setMode="OneWay"
– Kiriz
Mar 25 at 13:41
Ok, next question is what are you expect? I cannot see in your code any changes that affect UI. Just Update method where you do nothing just invoke Property changed. But none of the properties changed. You need to change property and only after that invoke property changed event
– Yauhen Sampir
Mar 26 at 6:55
add a comment |
Firstly, you need to specify the name of the variable that you want to update:
public void Update()
RaisePropertyChanged(nameof(Name));
Documentation and sample: https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.data.inotifypropertychanged.propertychanged
Secondly, by default x:Bind is OneTime
To fix it, add Mode="OneWay"
Mode Specifies the binding mode, as one of these strings: "OneTime", "OneWay", or "TwoWay". The default is "OneTime". Note that this differs from the default for Binding, which is "OneWay" in most cases.
Please read documentation
https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/x-bind-markup-extension
Firstly, you need to specify the name of the variable that you want to update:
public void Update()
RaisePropertyChanged(nameof(Name));
Documentation and sample: https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.data.inotifypropertychanged.propertychanged
Secondly, by default x:Bind is OneTime
To fix it, add Mode="OneWay"
Mode Specifies the binding mode, as one of these strings: "OneTime", "OneWay", or "TwoWay". The default is "OneTime". Note that this differs from the default for Binding, which is "OneWay" in most cases.
Please read documentation
https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/x-bind-markup-extension
edited Mar 25 at 12:33
answered Mar 25 at 12:27
Yauhen SampirYauhen Sampir
3202 silver badges9 bronze badges
3202 silver badges9 bronze badges
1.The PropertyChanged event can indicate that all properties on the object have changed by using String.Empty for the PropertyName property of the PropertyChangedEventArgs.
according to your documentation in first part. 2. I've already setMode="OneWay"
– Kiriz
Mar 25 at 13:41
Ok, next question is what are you expect? I cannot see in your code any changes that affect UI. Just Update method where you do nothing just invoke Property changed. But none of the properties changed. You need to change property and only after that invoke property changed event
– Yauhen Sampir
Mar 26 at 6:55
add a comment |
1.The PropertyChanged event can indicate that all properties on the object have changed by using String.Empty for the PropertyName property of the PropertyChangedEventArgs.
according to your documentation in first part. 2. I've already setMode="OneWay"
– Kiriz
Mar 25 at 13:41
Ok, next question is what are you expect? I cannot see in your code any changes that affect UI. Just Update method where you do nothing just invoke Property changed. But none of the properties changed. You need to change property and only after that invoke property changed event
– Yauhen Sampir
Mar 26 at 6:55
1.
The PropertyChanged event can indicate that all properties on the object have changed by using String.Empty for the PropertyName property of the PropertyChangedEventArgs.
according to your documentation in first part. 2. I've already set Mode="OneWay"
– Kiriz
Mar 25 at 13:41
1.
The PropertyChanged event can indicate that all properties on the object have changed by using String.Empty for the PropertyName property of the PropertyChangedEventArgs.
according to your documentation in first part. 2. I've already set Mode="OneWay"
– Kiriz
Mar 25 at 13:41
Ok, next question is what are you expect? I cannot see in your code any changes that affect UI. Just Update method where you do nothing just invoke Property changed. But none of the properties changed. You need to change property and only after that invoke property changed event
– Yauhen Sampir
Mar 26 at 6:55
Ok, next question is what are you expect? I cannot see in your code any changes that affect UI. Just Update method where you do nothing just invoke Property changed. But none of the properties changed. You need to change property and only after that invoke property changed event
– Yauhen Sampir
Mar 26 at 6:55
add a comment |
The problem here is not on the level of the StaffViewModel
class, but on the level of the page. When you do:
staffVM = staffListView.SelectedItem as StaffViewModel;
The UI has no notification about the fact that the staffVM
field has changed. So the binding is still pointing to the old instance of StaffViewModel
. Hence when you do staffVM.Update()
, it does notify about changes, but the UI is not listening to that instance - it is still listening to notifications on the first selected item. Bindings.Update()
fixes this because it completely re-evaluates all bindings so it will "get" the new value of staffVM
field.
Solution would be to implement INotifyPropertyChanged
on the Page
and encapsulate the staffVM
in a property which raises PropertyChanged
event.
Ideally I would however suggest creating a "root" view model, which you will set only once and will not change and which will contain the selected item as its property. This way you don't have to implement INotifyPropertyChanged
in the Page
and its code-behind will be simpler. As a result you will have something like the following in the code-behind:
public RootViewModel VM get; = new RootViewModel();
And in XAML:
<TextBlock Text="x:Bind VM.SelectedStaff.Name, Mode=OneWay"/>
add a comment |
The problem here is not on the level of the StaffViewModel
class, but on the level of the page. When you do:
staffVM = staffListView.SelectedItem as StaffViewModel;
The UI has no notification about the fact that the staffVM
field has changed. So the binding is still pointing to the old instance of StaffViewModel
. Hence when you do staffVM.Update()
, it does notify about changes, but the UI is not listening to that instance - it is still listening to notifications on the first selected item. Bindings.Update()
fixes this because it completely re-evaluates all bindings so it will "get" the new value of staffVM
field.
Solution would be to implement INotifyPropertyChanged
on the Page
and encapsulate the staffVM
in a property which raises PropertyChanged
event.
Ideally I would however suggest creating a "root" view model, which you will set only once and will not change and which will contain the selected item as its property. This way you don't have to implement INotifyPropertyChanged
in the Page
and its code-behind will be simpler. As a result you will have something like the following in the code-behind:
public RootViewModel VM get; = new RootViewModel();
And in XAML:
<TextBlock Text="x:Bind VM.SelectedStaff.Name, Mode=OneWay"/>
add a comment |
The problem here is not on the level of the StaffViewModel
class, but on the level of the page. When you do:
staffVM = staffListView.SelectedItem as StaffViewModel;
The UI has no notification about the fact that the staffVM
field has changed. So the binding is still pointing to the old instance of StaffViewModel
. Hence when you do staffVM.Update()
, it does notify about changes, but the UI is not listening to that instance - it is still listening to notifications on the first selected item. Bindings.Update()
fixes this because it completely re-evaluates all bindings so it will "get" the new value of staffVM
field.
Solution would be to implement INotifyPropertyChanged
on the Page
and encapsulate the staffVM
in a property which raises PropertyChanged
event.
Ideally I would however suggest creating a "root" view model, which you will set only once and will not change and which will contain the selected item as its property. This way you don't have to implement INotifyPropertyChanged
in the Page
and its code-behind will be simpler. As a result you will have something like the following in the code-behind:
public RootViewModel VM get; = new RootViewModel();
And in XAML:
<TextBlock Text="x:Bind VM.SelectedStaff.Name, Mode=OneWay"/>
The problem here is not on the level of the StaffViewModel
class, but on the level of the page. When you do:
staffVM = staffListView.SelectedItem as StaffViewModel;
The UI has no notification about the fact that the staffVM
field has changed. So the binding is still pointing to the old instance of StaffViewModel
. Hence when you do staffVM.Update()
, it does notify about changes, but the UI is not listening to that instance - it is still listening to notifications on the first selected item. Bindings.Update()
fixes this because it completely re-evaluates all bindings so it will "get" the new value of staffVM
field.
Solution would be to implement INotifyPropertyChanged
on the Page
and encapsulate the staffVM
in a property which raises PropertyChanged
event.
Ideally I would however suggest creating a "root" view model, which you will set only once and will not change and which will contain the selected item as its property. This way you don't have to implement INotifyPropertyChanged
in the Page
and its code-behind will be simpler. As a result you will have something like the following in the code-behind:
public RootViewModel VM get; = new RootViewModel();
And in XAML:
<TextBlock Text="x:Bind VM.SelectedStaff.Name, Mode=OneWay"/>
answered Mar 25 at 16:10
Martin ZikmundMartin Zikmund
27.2k6 gold badges42 silver badges65 bronze badges
27.2k6 gold badges42 silver badges65 bronze badges
add a comment |
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%2f55337531%2fxbind-ui-not-update-when-propertychanged-in-uwp%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