属性更改(UWP)上的数据绑定不会更新

时间:2017-04-16 23:33:33

标签: c# xaml data-binding uwp

我正在使用c#和通用Windows平台(UWP)开发应用程序,并且正在努力在布局控件和可观察类之间创建单向数据绑定。目前,当更改observable类属性时,它不会更新UI元素。我认为它与我绑定DataTemplate ListViewItem而不是静态布局元素的事实有关,但我不确定这是问题还是如何解决它。任何帮助,将不胜感激。显示了UI元素和后端代码的代码。

DataTemplate(XAML)(为了便于阅读,删除了样式)

<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(删除了不相关的属性)

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代码

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

再次感谢您的帮助,我知道它的代码很多,我非常感谢您在调试/学习方面的帮助。

1 个答案:

答案 0 :(得分:4)

好的,所以我最终找到了答案,我认为这可能有助于其他人试图复制我想要做的事情:

基本上,一个人试图使其可观察的类必须扩展类INotifyPropertyChanged。所以,我最终创建了一个基类,从中扩展了我的所有可观察类:

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