2017-04-16 278 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)); 
    } 
}