Problem with Dependency Property and PropertyChangedCallback in WPF
In my CustomControl, I have the View dependency property. The outer control that uses this control sets this property via a view model. When it is set, a Refresh method is trigerred and the View is rendered!
Everything is fine so far! But what if I also want it to Refresh when a property of the View is changed.
Maybe I am not doing it the standard way? Should I define a public Refresh() method on the control and call it from outside? How can I use commanding?
public static readonly DependencyProperty ViewProperty =
DependencyProperty.Register(
"View", typeof(View),
typeof(CustomControl), new PropertyMetadata(Refresh)
);
public View View
get => (View)GetValue(ViewProperty);
set => SetValue(ViewProperty, value);
private static void Refresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
//
MessageBox.Show("Refreshed!");
public sealed class View : INotifyPropertyChanged
private bool m_isDirty;
public bool IsDirty
get => m_isDirty;
set
m_isDirty = value;
OnPropertyChanged();
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
c# wpf mvvm dependency-properties inotifypropertychanged
add a comment |
In my CustomControl, I have the View dependency property. The outer control that uses this control sets this property via a view model. When it is set, a Refresh method is trigerred and the View is rendered!
Everything is fine so far! But what if I also want it to Refresh when a property of the View is changed.
Maybe I am not doing it the standard way? Should I define a public Refresh() method on the control and call it from outside? How can I use commanding?
public static readonly DependencyProperty ViewProperty =
DependencyProperty.Register(
"View", typeof(View),
typeof(CustomControl), new PropertyMetadata(Refresh)
);
public View View
get => (View)GetValue(ViewProperty);
set => SetValue(ViewProperty, value);
private static void Refresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
//
MessageBox.Show("Refreshed!");
public sealed class View : INotifyPropertyChanged
private bool m_isDirty;
public bool IsDirty
get => m_isDirty;
set
m_isDirty = value;
OnPropertyChanged();
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
c# wpf mvvm dependency-properties inotifypropertychanged
add a comment |
In my CustomControl, I have the View dependency property. The outer control that uses this control sets this property via a view model. When it is set, a Refresh method is trigerred and the View is rendered!
Everything is fine so far! But what if I also want it to Refresh when a property of the View is changed.
Maybe I am not doing it the standard way? Should I define a public Refresh() method on the control and call it from outside? How can I use commanding?
public static readonly DependencyProperty ViewProperty =
DependencyProperty.Register(
"View", typeof(View),
typeof(CustomControl), new PropertyMetadata(Refresh)
);
public View View
get => (View)GetValue(ViewProperty);
set => SetValue(ViewProperty, value);
private static void Refresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
//
MessageBox.Show("Refreshed!");
public sealed class View : INotifyPropertyChanged
private bool m_isDirty;
public bool IsDirty
get => m_isDirty;
set
m_isDirty = value;
OnPropertyChanged();
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
c# wpf mvvm dependency-properties inotifypropertychanged
In my CustomControl, I have the View dependency property. The outer control that uses this control sets this property via a view model. When it is set, a Refresh method is trigerred and the View is rendered!
Everything is fine so far! But what if I also want it to Refresh when a property of the View is changed.
Maybe I am not doing it the standard way? Should I define a public Refresh() method on the control and call it from outside? How can I use commanding?
public static readonly DependencyProperty ViewProperty =
DependencyProperty.Register(
"View", typeof(View),
typeof(CustomControl), new PropertyMetadata(Refresh)
);
public View View
get => (View)GetValue(ViewProperty);
set => SetValue(ViewProperty, value);
private static void Refresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
//
MessageBox.Show("Refreshed!");
public sealed class View : INotifyPropertyChanged
private bool m_isDirty;
public bool IsDirty
get => m_isDirty;
set
m_isDirty = value;
OnPropertyChanged();
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
c# wpf mvvm dependency-properties inotifypropertychanged
c# wpf mvvm dependency-properties inotifypropertychanged
edited Nov 15 '18 at 6:16
Blacktempel
2,88831739
2,88831739
asked Nov 14 '18 at 13:13
VahidVahid
1,91953784
1,91953784
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You could hook up an event handler for the PropertyChanged
event of the View
in your callback:
private static void Refresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
MessageBox.Show("Refreshed!");
View newView = e.NewValue as View;
if (newView != null)
newView.PropertyChanged += NewView_PropertyChanged;
View oldView = e.OldValue as View;
if (oldView != null)
oldView.PropertyChanged -= NewView_PropertyChanged;
private static void NewView_PropertyChanged(object sender, PropertyChangedEventArgs e)
View view = (View)sender;
//view updated...
Sounds a reasonable solution. Thank you. But then it will fire on every property change in View! And I do not want to hard-code the property name here! Isn't it possible to have some Refresh() method in the Control and a mechansim so that I can force it to execute via binding from outside?
– Vahid
Nov 14 '18 at 13:37
It will fire whenever the view fires the PropertyChanged event. What property name are you referring to?
– mm8
Nov 14 '18 at 13:41
My View may have other properties that raise OnPropertyChanged like Name, etc. I only want change in a specific property of View to trigger the Refresh() on CustomControl. What I want is a kind of mechansim in CustomControl so that I can bind to my ViewModel outside the Control.
– Vahid
Nov 14 '18 at 13:43
Maybe I should have a boolean RefreshRequested DP in CustomControl and bind it to imy view model outside the control as well?
– Vahid
Nov 14 '18 at 13:44
Either check the name of the property that raised the event in the control or raise another event in View.
– mm8
Nov 14 '18 at 13:54
|
show 1 more 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%2f53301076%2fproblem-with-dependency-property-and-propertychangedcallback-in-wpf%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
You could hook up an event handler for the PropertyChanged
event of the View
in your callback:
private static void Refresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
MessageBox.Show("Refreshed!");
View newView = e.NewValue as View;
if (newView != null)
newView.PropertyChanged += NewView_PropertyChanged;
View oldView = e.OldValue as View;
if (oldView != null)
oldView.PropertyChanged -= NewView_PropertyChanged;
private static void NewView_PropertyChanged(object sender, PropertyChangedEventArgs e)
View view = (View)sender;
//view updated...
Sounds a reasonable solution. Thank you. But then it will fire on every property change in View! And I do not want to hard-code the property name here! Isn't it possible to have some Refresh() method in the Control and a mechansim so that I can force it to execute via binding from outside?
– Vahid
Nov 14 '18 at 13:37
It will fire whenever the view fires the PropertyChanged event. What property name are you referring to?
– mm8
Nov 14 '18 at 13:41
My View may have other properties that raise OnPropertyChanged like Name, etc. I only want change in a specific property of View to trigger the Refresh() on CustomControl. What I want is a kind of mechansim in CustomControl so that I can bind to my ViewModel outside the Control.
– Vahid
Nov 14 '18 at 13:43
Maybe I should have a boolean RefreshRequested DP in CustomControl and bind it to imy view model outside the control as well?
– Vahid
Nov 14 '18 at 13:44
Either check the name of the property that raised the event in the control or raise another event in View.
– mm8
Nov 14 '18 at 13:54
|
show 1 more comment
You could hook up an event handler for the PropertyChanged
event of the View
in your callback:
private static void Refresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
MessageBox.Show("Refreshed!");
View newView = e.NewValue as View;
if (newView != null)
newView.PropertyChanged += NewView_PropertyChanged;
View oldView = e.OldValue as View;
if (oldView != null)
oldView.PropertyChanged -= NewView_PropertyChanged;
private static void NewView_PropertyChanged(object sender, PropertyChangedEventArgs e)
View view = (View)sender;
//view updated...
Sounds a reasonable solution. Thank you. But then it will fire on every property change in View! And I do not want to hard-code the property name here! Isn't it possible to have some Refresh() method in the Control and a mechansim so that I can force it to execute via binding from outside?
– Vahid
Nov 14 '18 at 13:37
It will fire whenever the view fires the PropertyChanged event. What property name are you referring to?
– mm8
Nov 14 '18 at 13:41
My View may have other properties that raise OnPropertyChanged like Name, etc. I only want change in a specific property of View to trigger the Refresh() on CustomControl. What I want is a kind of mechansim in CustomControl so that I can bind to my ViewModel outside the Control.
– Vahid
Nov 14 '18 at 13:43
Maybe I should have a boolean RefreshRequested DP in CustomControl and bind it to imy view model outside the control as well?
– Vahid
Nov 14 '18 at 13:44
Either check the name of the property that raised the event in the control or raise another event in View.
– mm8
Nov 14 '18 at 13:54
|
show 1 more comment
You could hook up an event handler for the PropertyChanged
event of the View
in your callback:
private static void Refresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
MessageBox.Show("Refreshed!");
View newView = e.NewValue as View;
if (newView != null)
newView.PropertyChanged += NewView_PropertyChanged;
View oldView = e.OldValue as View;
if (oldView != null)
oldView.PropertyChanged -= NewView_PropertyChanged;
private static void NewView_PropertyChanged(object sender, PropertyChangedEventArgs e)
View view = (View)sender;
//view updated...
You could hook up an event handler for the PropertyChanged
event of the View
in your callback:
private static void Refresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
MessageBox.Show("Refreshed!");
View newView = e.NewValue as View;
if (newView != null)
newView.PropertyChanged += NewView_PropertyChanged;
View oldView = e.OldValue as View;
if (oldView != null)
oldView.PropertyChanged -= NewView_PropertyChanged;
private static void NewView_PropertyChanged(object sender, PropertyChangedEventArgs e)
View view = (View)sender;
//view updated...
answered Nov 14 '18 at 13:22
mm8mm8
84.1k81931
84.1k81931
Sounds a reasonable solution. Thank you. But then it will fire on every property change in View! And I do not want to hard-code the property name here! Isn't it possible to have some Refresh() method in the Control and a mechansim so that I can force it to execute via binding from outside?
– Vahid
Nov 14 '18 at 13:37
It will fire whenever the view fires the PropertyChanged event. What property name are you referring to?
– mm8
Nov 14 '18 at 13:41
My View may have other properties that raise OnPropertyChanged like Name, etc. I only want change in a specific property of View to trigger the Refresh() on CustomControl. What I want is a kind of mechansim in CustomControl so that I can bind to my ViewModel outside the Control.
– Vahid
Nov 14 '18 at 13:43
Maybe I should have a boolean RefreshRequested DP in CustomControl and bind it to imy view model outside the control as well?
– Vahid
Nov 14 '18 at 13:44
Either check the name of the property that raised the event in the control or raise another event in View.
– mm8
Nov 14 '18 at 13:54
|
show 1 more comment
Sounds a reasonable solution. Thank you. But then it will fire on every property change in View! And I do not want to hard-code the property name here! Isn't it possible to have some Refresh() method in the Control and a mechansim so that I can force it to execute via binding from outside?
– Vahid
Nov 14 '18 at 13:37
It will fire whenever the view fires the PropertyChanged event. What property name are you referring to?
– mm8
Nov 14 '18 at 13:41
My View may have other properties that raise OnPropertyChanged like Name, etc. I only want change in a specific property of View to trigger the Refresh() on CustomControl. What I want is a kind of mechansim in CustomControl so that I can bind to my ViewModel outside the Control.
– Vahid
Nov 14 '18 at 13:43
Maybe I should have a boolean RefreshRequested DP in CustomControl and bind it to imy view model outside the control as well?
– Vahid
Nov 14 '18 at 13:44
Either check the name of the property that raised the event in the control or raise another event in View.
– mm8
Nov 14 '18 at 13:54
Sounds a reasonable solution. Thank you. But then it will fire on every property change in View! And I do not want to hard-code the property name here! Isn't it possible to have some Refresh() method in the Control and a mechansim so that I can force it to execute via binding from outside?
– Vahid
Nov 14 '18 at 13:37
Sounds a reasonable solution. Thank you. But then it will fire on every property change in View! And I do not want to hard-code the property name here! Isn't it possible to have some Refresh() method in the Control and a mechansim so that I can force it to execute via binding from outside?
– Vahid
Nov 14 '18 at 13:37
It will fire whenever the view fires the PropertyChanged event. What property name are you referring to?
– mm8
Nov 14 '18 at 13:41
It will fire whenever the view fires the PropertyChanged event. What property name are you referring to?
– mm8
Nov 14 '18 at 13:41
My View may have other properties that raise OnPropertyChanged like Name, etc. I only want change in a specific property of View to trigger the Refresh() on CustomControl. What I want is a kind of mechansim in CustomControl so that I can bind to my ViewModel outside the Control.
– Vahid
Nov 14 '18 at 13:43
My View may have other properties that raise OnPropertyChanged like Name, etc. I only want change in a specific property of View to trigger the Refresh() on CustomControl. What I want is a kind of mechansim in CustomControl so that I can bind to my ViewModel outside the Control.
– Vahid
Nov 14 '18 at 13:43
Maybe I should have a boolean RefreshRequested DP in CustomControl and bind it to imy view model outside the control as well?
– Vahid
Nov 14 '18 at 13:44
Maybe I should have a boolean RefreshRequested DP in CustomControl and bind it to imy view model outside the control as well?
– Vahid
Nov 14 '18 at 13:44
Either check the name of the property that raised the event in the control or raise another event in View.
– mm8
Nov 14 '18 at 13:54
Either check the name of the property that raised the event in the control or raise another event in View.
– mm8
Nov 14 '18 at 13:54
|
show 1 more 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%2f53301076%2fproblem-with-dependency-property-and-propertychangedcallback-in-wpf%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