2017-04-16 183 views
3

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

的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#可观察类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"; 
} 

再次感谢您的帮助,我知道它的很多代码,我很赞赏e帮助调试/学习。

+0

'(...)绑定一个DataTemplate ListViewItem的而不是静态的布局元素(...)'这应该不是问题。是否显示其他绑定(例如“{绑定描述}”)?你有尝试切换'绑定:绑定''绑定'(请参阅这里为什么这可能很重要:http://stackoverflow.com/questions/33070705/with-compiled-bindings-xbind-why-do-i-have-to -call-绑定更新) –

回答

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