Nick
Nick

Reputation: 783

My UserControl's TextBlock binding doesn't update even once

I know this has been asked for many times. I read a lot of them and tried different ways but still could not get it to work.

The xaml code is a UserControl:

<Grid Name="middle">
    <d:TextBlock Text="{x:Bind LayerNodeData.CleanName, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Foreground="WhiteSmoke" FontSize="12" FontFamily="Arial" VerticalAlignment="Center" RelativePanel.RightOf="visibleUI" DoubleTapped="OnEditNameBegin" />
</Grid>

I set both this.DataContext and the Grid's DataContext to the data instance.

c#

public ucLayerRow(ImageLayerNode data)
{
        LayerNodeData = data;

        DataContext = LayerNodeData;

        this.InitializeComponent();

        middle.DataContext = LayerNodeData;

        LayerNodeData.NotifyPropertyChanged("CleanName"); // test if it work

        RefreshUI();
}

Model class

public partial class ImageLayerNode : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            // PropertyChanged is always null.
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
        
    public string mCleanName = string.Empty;
    public string CleanName { 
        get => mCleanName;
        set { mCleanName = value; NotifyPropertyChanged();}
    }
    ....
}

I tried add a breakpoint to the PropertyChanged and found that it is always null and thus never get called. I also tried changing the mode to OneWay, TwoWays but still nothing.

The textblock is away empty not even getting a value once.

The user control is added like this to the main page. Not sure if it is related.

var rowUI = new ucLayerRow(layerNode);                        
layerContainer.Children.Add(rowUI);

Upvotes: 1

Views: 72

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

My UserControl's TextBlock binding doesn't update even once

During the testing, the problem looks that you use design time for usercontrol. <d:TextBlock/> please remove d: and make your usercontrol like the following.

Xaml

<Grid>
    <TextBlock
        VerticalAlignment="Center"
        FontFamily="Arial"
        FontSize="12"
        Foreground="Red"
        Text="{x:Bind LayerNodeData.CleanName, Mode=OneWay}" />
</Grid>

Code behind

public sealed partial class ucLayerRow : UserControl
{
    public ucLayerRow(ImageLayerNode data)
    {
        this.InitializeComponent();
        LayerNodeData = data;
    }
 
    public ImageLayerNode LayerNodeData { get; set; }
}
public partial class ImageLayerNode : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            // PropertyChanged is always null.
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private string mCleanName = string.Empty;
    public string CleanName
    {
        get => mCleanName;
        set { mCleanName = value; NotifyPropertyChanged(); }
    }

}

Upvotes: 1

Related Questions