Schrodinger object: WPF property will not bind / update unless I check its value on the code side?

I have a Node object with a parent that I linked in my xaml, e.g.

<Label>
    <Hyperlink>
        <TextBlock Text="{Binding Path=Node.Parent.Name}"/>
    </Hyperlink>
</Label>

My ViewModel looks something like this:

public class NodeViewModel
{
    public Node Node { get; set; }

    public NodeViewModel(Node model)
    {
        Node = model;
        if(model.Parent != null) { } // Check if it null, then do nothing.
        // When the above line is commented out, my label displays nothing.
    }

}

Why is this when the if if statement is commented out, the label / text block is empty? Am I doing something wrong? Is my object both existing and not existing until I check if it is null?

Edit:
Remember, my Node class is pretty simple and implements INotifyPropertyChangedfor the Name property.

2nd Edit: Added my simple Node class.

[ImplementPropertyChanged] // From Fody.PropertyChanged
public class Node
{
    public int? ParentID { get; set; }
    public Node Parent { get; set; }
    public string Name { get; set; }

    public Node Node(Node p = null)
    {
        Parent = p;
    }
}
+4
source share
2 answers

From the comments:

. Entity Framework. ? , ?

, , , . - WPF .

WPF -. . , , . , , . , INPC:

void PropertyChanged(object sender, .... args)

WPF sender Binding.Source. "Z", - "A", , "A", - "Z" WPF, , WPF "Z" , "A".

, , , - P-Changed, sender=A Z. , . , "" . , - . . --, sender -replacement .

- - pleease, !

, . . "Z", "A". . "" //, , -, . "" , A, - Z:A, WPF Source sender. , EF.

, , - , . , Fody not EF? ImplementPropertyChanged, INPC . . Node, NodeViewModel. , propdp DependencyProperty. - INPC ie. . , .

, , Fody - , - - ?

EDIT: , , Fody'ied, . , , NodeViewModel.Node ? Fody? ImplementPropertyChanged ? , , , .

+3

, INPC INPC ViewModel. Node = node UI. property changed Node.

0

Source: https://habr.com/ru/post/1532894/


All Articles