Can't bind to a member of a Model from a ViewModel












1















MyProduct is the model that has HasError boolean property (with OnPropertyChanged ...) that can change.
MyProductDialogViewModel is:



class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }
public MyProduct Product { get; set; }
public bool HasError
{
get { return Product.HasError; }
}
}


I have assigned MyProductDialogViewModel instance to BaseContentControl.DataContext to inflate a ContentControl.
This View can be inflated with different ViewModels all having HasError property using template binding.



<ContentControl x:Name="BaseContentControl" Content="{Binding}" ... >


Then I try to extract informations directly from its DataContext.
This don't work:



<Label Content="{Binding ElementName=BaseContentControl, Path=DataContext.HasError}"/>


But this works perfectly.



<Label Content="{Binding ElementName=BaseContentControl, Path=DataContext.Product.HasError}"/>


I tought it ca be a notifiy problem in the ViewModel so I have changed to this:



class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }

public MyProduct Product { get; set; }

public bool HasError
{
get { return Product.HasError; }
set
{
if (Product.HasError != value)
{
Product.HasError = value;
OnPropertyChanged("HasError");
}
}
}
}


but to no avail (in fact the set method is never called so it never notifies).



I don't want to directly refer to the specific Model instance cause the View can be inflated with different ViewModels.
How can I do ?



Thanks










share|improve this question




















  • 2





    Does Prouduct class implements INotifypropertychanged and HasError calls OnPropertyChanged event ?

    – Satish Pai
    Nov 22 '18 at 11:18











  • Yes, it does implements INotifypropertychanged and calls OnPropertyChanged("HasError")

    – Beorne
    Nov 22 '18 at 12:52
















1















MyProduct is the model that has HasError boolean property (with OnPropertyChanged ...) that can change.
MyProductDialogViewModel is:



class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }
public MyProduct Product { get; set; }
public bool HasError
{
get { return Product.HasError; }
}
}


I have assigned MyProductDialogViewModel instance to BaseContentControl.DataContext to inflate a ContentControl.
This View can be inflated with different ViewModels all having HasError property using template binding.



<ContentControl x:Name="BaseContentControl" Content="{Binding}" ... >


Then I try to extract informations directly from its DataContext.
This don't work:



<Label Content="{Binding ElementName=BaseContentControl, Path=DataContext.HasError}"/>


But this works perfectly.



<Label Content="{Binding ElementName=BaseContentControl, Path=DataContext.Product.HasError}"/>


I tought it ca be a notifiy problem in the ViewModel so I have changed to this:



class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }

public MyProduct Product { get; set; }

public bool HasError
{
get { return Product.HasError; }
set
{
if (Product.HasError != value)
{
Product.HasError = value;
OnPropertyChanged("HasError");
}
}
}
}


but to no avail (in fact the set method is never called so it never notifies).



I don't want to directly refer to the specific Model instance cause the View can be inflated with different ViewModels.
How can I do ?



Thanks










share|improve this question




















  • 2





    Does Prouduct class implements INotifypropertychanged and HasError calls OnPropertyChanged event ?

    – Satish Pai
    Nov 22 '18 at 11:18











  • Yes, it does implements INotifypropertychanged and calls OnPropertyChanged("HasError")

    – Beorne
    Nov 22 '18 at 12:52














1












1








1


2






MyProduct is the model that has HasError boolean property (with OnPropertyChanged ...) that can change.
MyProductDialogViewModel is:



class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }
public MyProduct Product { get; set; }
public bool HasError
{
get { return Product.HasError; }
}
}


I have assigned MyProductDialogViewModel instance to BaseContentControl.DataContext to inflate a ContentControl.
This View can be inflated with different ViewModels all having HasError property using template binding.



<ContentControl x:Name="BaseContentControl" Content="{Binding}" ... >


Then I try to extract informations directly from its DataContext.
This don't work:



<Label Content="{Binding ElementName=BaseContentControl, Path=DataContext.HasError}"/>


But this works perfectly.



<Label Content="{Binding ElementName=BaseContentControl, Path=DataContext.Product.HasError}"/>


I tought it ca be a notifiy problem in the ViewModel so I have changed to this:



class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }

public MyProduct Product { get; set; }

public bool HasError
{
get { return Product.HasError; }
set
{
if (Product.HasError != value)
{
Product.HasError = value;
OnPropertyChanged("HasError");
}
}
}
}


but to no avail (in fact the set method is never called so it never notifies).



I don't want to directly refer to the specific Model instance cause the View can be inflated with different ViewModels.
How can I do ?



Thanks










share|improve this question
















MyProduct is the model that has HasError boolean property (with OnPropertyChanged ...) that can change.
MyProductDialogViewModel is:



class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }
public MyProduct Product { get; set; }
public bool HasError
{
get { return Product.HasError; }
}
}


I have assigned MyProductDialogViewModel instance to BaseContentControl.DataContext to inflate a ContentControl.
This View can be inflated with different ViewModels all having HasError property using template binding.



<ContentControl x:Name="BaseContentControl" Content="{Binding}" ... >


Then I try to extract informations directly from its DataContext.
This don't work:



<Label Content="{Binding ElementName=BaseContentControl, Path=DataContext.HasError}"/>


But this works perfectly.



<Label Content="{Binding ElementName=BaseContentControl, Path=DataContext.Product.HasError}"/>


I tought it ca be a notifiy problem in the ViewModel so I have changed to this:



class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }

public MyProduct Product { get; set; }

public bool HasError
{
get { return Product.HasError; }
set
{
if (Product.HasError != value)
{
Product.HasError = value;
OnPropertyChanged("HasError");
}
}
}
}


but to no avail (in fact the set method is never called so it never notifies).



I don't want to directly refer to the specific Model instance cause the View can be inflated with different ViewModels.
How can I do ?



Thanks







c# wpf mvvm data-binding inotifypropertychanged






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 13:04







Beorne

















asked Nov 22 '18 at 11:03









BeorneBeorne

68111




68111








  • 2





    Does Prouduct class implements INotifypropertychanged and HasError calls OnPropertyChanged event ?

    – Satish Pai
    Nov 22 '18 at 11:18











  • Yes, it does implements INotifypropertychanged and calls OnPropertyChanged("HasError")

    – Beorne
    Nov 22 '18 at 12:52














  • 2





    Does Prouduct class implements INotifypropertychanged and HasError calls OnPropertyChanged event ?

    – Satish Pai
    Nov 22 '18 at 11:18











  • Yes, it does implements INotifypropertychanged and calls OnPropertyChanged("HasError")

    – Beorne
    Nov 22 '18 at 12:52








2




2





Does Prouduct class implements INotifypropertychanged and HasError calls OnPropertyChanged event ?

– Satish Pai
Nov 22 '18 at 11:18





Does Prouduct class implements INotifypropertychanged and HasError calls OnPropertyChanged event ?

– Satish Pai
Nov 22 '18 at 11:18













Yes, it does implements INotifypropertychanged and calls OnPropertyChanged("HasError")

– Beorne
Nov 22 '18 at 12:52





Yes, it does implements INotifypropertychanged and calls OnPropertyChanged("HasError")

– Beorne
Nov 22 '18 at 12:52












1 Answer
1






active

oldest

votes


















1














You have to propagate the PropertyChanged event of MyProduct, i.e. subscribe to it and invoke OnPropertyChanged(nameof(HasError)) if HasError property of MyProduct being changed:



public class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }

private MyProduct _product = null;
public MyProduct Product
{
get { return _product; }
set
{
if (_product!=null)
{
_product.PropertyChanged -= Product_PropertyChanged;
}
_product = value;
if (_product != null)
{
_product.PropertyChanged += Product_PropertyChanged;
}
}
}

private void Product_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName==nameof(MyProduct.HasError))
{
OnPropertyChanged(nameof(HasError));
}
}

public bool HasError => Product.HasError;
}





share|improve this answer





















  • 1





    Perfect. You should change Product_PropertyChanged in code to ViewModel_PropertyChanged

    – Beorne
    Nov 22 '18 at 13:30











  • @Beorne Done.. :)

    – Rekshino
    Nov 22 '18 at 13:32











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429531%2fcant-bind-to-a-member-of-a-model-from-a-viewmodel%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









1














You have to propagate the PropertyChanged event of MyProduct, i.e. subscribe to it and invoke OnPropertyChanged(nameof(HasError)) if HasError property of MyProduct being changed:



public class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }

private MyProduct _product = null;
public MyProduct Product
{
get { return _product; }
set
{
if (_product!=null)
{
_product.PropertyChanged -= Product_PropertyChanged;
}
_product = value;
if (_product != null)
{
_product.PropertyChanged += Product_PropertyChanged;
}
}
}

private void Product_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName==nameof(MyProduct.HasError))
{
OnPropertyChanged(nameof(HasError));
}
}

public bool HasError => Product.HasError;
}





share|improve this answer





















  • 1





    Perfect. You should change Product_PropertyChanged in code to ViewModel_PropertyChanged

    – Beorne
    Nov 22 '18 at 13:30











  • @Beorne Done.. :)

    – Rekshino
    Nov 22 '18 at 13:32
















1














You have to propagate the PropertyChanged event of MyProduct, i.e. subscribe to it and invoke OnPropertyChanged(nameof(HasError)) if HasError property of MyProduct being changed:



public class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }

private MyProduct _product = null;
public MyProduct Product
{
get { return _product; }
set
{
if (_product!=null)
{
_product.PropertyChanged -= Product_PropertyChanged;
}
_product = value;
if (_product != null)
{
_product.PropertyChanged += Product_PropertyChanged;
}
}
}

private void Product_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName==nameof(MyProduct.HasError))
{
OnPropertyChanged(nameof(HasError));
}
}

public bool HasError => Product.HasError;
}





share|improve this answer





















  • 1





    Perfect. You should change Product_PropertyChanged in code to ViewModel_PropertyChanged

    – Beorne
    Nov 22 '18 at 13:30











  • @Beorne Done.. :)

    – Rekshino
    Nov 22 '18 at 13:32














1












1








1







You have to propagate the PropertyChanged event of MyProduct, i.e. subscribe to it and invoke OnPropertyChanged(nameof(HasError)) if HasError property of MyProduct being changed:



public class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }

private MyProduct _product = null;
public MyProduct Product
{
get { return _product; }
set
{
if (_product!=null)
{
_product.PropertyChanged -= Product_PropertyChanged;
}
_product = value;
if (_product != null)
{
_product.PropertyChanged += Product_PropertyChanged;
}
}
}

private void Product_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName==nameof(MyProduct.HasError))
{
OnPropertyChanged(nameof(HasError));
}
}

public bool HasError => Product.HasError;
}





share|improve this answer















You have to propagate the PropertyChanged event of MyProduct, i.e. subscribe to it and invoke OnPropertyChanged(nameof(HasError)) if HasError property of MyProduct being changed:



public class ProductDialogViewModel : Notifier
{
public ProductDialogViewModel() { }

private MyProduct _product = null;
public MyProduct Product
{
get { return _product; }
set
{
if (_product!=null)
{
_product.PropertyChanged -= Product_PropertyChanged;
}
_product = value;
if (_product != null)
{
_product.PropertyChanged += Product_PropertyChanged;
}
}
}

private void Product_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName==nameof(MyProduct.HasError))
{
OnPropertyChanged(nameof(HasError));
}
}

public bool HasError => Product.HasError;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 13:31

























answered Nov 22 '18 at 11:41









RekshinoRekshino

2,9872932




2,9872932








  • 1





    Perfect. You should change Product_PropertyChanged in code to ViewModel_PropertyChanged

    – Beorne
    Nov 22 '18 at 13:30











  • @Beorne Done.. :)

    – Rekshino
    Nov 22 '18 at 13:32














  • 1





    Perfect. You should change Product_PropertyChanged in code to ViewModel_PropertyChanged

    – Beorne
    Nov 22 '18 at 13:30











  • @Beorne Done.. :)

    – Rekshino
    Nov 22 '18 at 13:32








1




1





Perfect. You should change Product_PropertyChanged in code to ViewModel_PropertyChanged

– Beorne
Nov 22 '18 at 13:30





Perfect. You should change Product_PropertyChanged in code to ViewModel_PropertyChanged

– Beorne
Nov 22 '18 at 13:30













@Beorne Done.. :)

– Rekshino
Nov 22 '18 at 13:32





@Beorne Done.. :)

– Rekshino
Nov 22 '18 at 13:32




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429531%2fcant-bind-to-a-member-of-a-model-from-a-viewmodel%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()