Data binding does not update when properties change (UWP)

I am developing an app using C # and the Universal Windows Platform (UWP) and am struggling with creating a one-way data binding between a layout control and an observable class. Currently, when a property of an observable class changes, it does not update the UI element. I think it has to do with the fact that I am binding the DataTemplate of the ListViewItem and not to a static layout item, but I'm not sure if this is the problem or how to solve it. Any help would be greatly appreciated. The code for the interface element and backend code is displayed.

DataTemplate (XAML) (styling removed for readability)

<DataTemplate x:Key="variableTemplate"
              x:DataType="local:VariableNode">
    <Border>
        <StackPanel Orientation="Vertical">
            <Border>
                <Grid>
                    <TextBlock Text="{Binding Name}" />
                    <StackPanel Orientation="Horizontal" >
                        <Button Tag="{Binding Description}"/>
                        <Button Tag="{Binding}"/>
                    </StackPanel>
                </Grid>
            </Border>
            <Grid Margin="0, 10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                </Grid.ColumnDefinitions>
                <Border >
                    <Grid Grid.Column="0">
                        <Button Click="Choose_Measurement"
                                Tag="{Binding}">
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="{x:Bind Path=Measurement_Name, Mode=TwoWay}"
                                           Foreground="{x:Bind MF}" />
                                <TextBlock Foreground="{x:Bind MF}" />
                            </StackPanel>
                        </Button>
                    </Grid>
                </Border>
                <Grid Grid.Column="1">
                    <Button Foreground="{Binding UF}"
                            Tag="{Binding}"
                            IsEnabled="{Binding Unit_Exists}"
                            Click="Choose_Unit">
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="{x:Bind Path=Unit_Name, Mode=OneWay}"
                                       Foreground="{Binding UF}" />
                                <TextBlock Foreground="{Binding UF}" />
                        </StackPanel>
                    </Button>
                </Grid>
            </Grid>
        </StackPanel>
    </Border>
</DataTemplate>

      

C # Observable class VariableNode (Exceptional properties removed)

public class VariableNode : ExperimentNode
{
    public VariableNode() { }
    public VariableNode(VariableType type)
    {
        Type = type;
        Name = name_ref[(int)Type];
        Category = "Problem";
        Unit = -1;
    }

    private string[] name_ref = { "Independent Variable", "Dependent Variable", "Controlled Variable" };
    public enum VariableType { Independent, Dependent, Controlled };
    public VariableType Type { get; set; }
    public Measurement Measure { get; set; }
    public int Unit { get; set; }

    [XmlIgnoreAttribute]
    public Measurement MeasureSource
    {
        get { return this.Measure; }
        set
        {
            this.Measure = value;
            OnPropertyChanged("Measurement_Name");
        }
    }
    [XmlIgnoreAttribute]
    public string Measurement_Name
    {
        get
        {
            if (Measure == null) { return "Select a Measurement"; }
            else { return Measure.Name; }
        }
        set
        {
            if (Measure != null)
            {
                Measure.Name = value;
                OnPropertyChanged();
            }                
        }
    }
    [XmlIgnoreAttribute]
    public string Unit_Name
    {
        get
        {
            if (Measure == null) { return "No measurement"; }
            else if (Unit < 0) { return "Select a unit"; }
            else { return Measure.Unit[Unit]; }
        }
    }
    [XmlIgnoreAttribute]
    public bool Unit_Exists
    {
        get { return Measure != null; }
    }

}

      

C # XAML.CS code causing property change

public void Choose_Measurement (object sender, RoutedEventArgs e) 
{
    Button butt = sender as Button
    VariableNode sel = butt.Tag as VariableNode;
    sel.Measurement_Name = "New Name";
}

      

Thanks again for the help, I know a lot of her code and I appreciate the help with debugging / learning.

+3


source to share


1 answer


Ok, so I found the answer and I think it might help other attempts to replicate what I am trying to do:

Basically, the class that is trying to make it visible should extend the class INotifyPropertyChanged

. So, I ended up creating a base class from which all my observable classes can be extended:



public class BaseClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChanged(this, e);
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }
}

      

+4


source







All Articles