2016-04-18 43 views
1

我正在嘗試顯示ObservableCollection(myClass)中的項目。類本身只有一些公共字符串屬性。我知道集合正在從流源正確更新,但由於某些原因,它並未使用我想要的屬性更新列表框。這很可能是我的XAML有一些錯誤:從數據綁定中顯示列表框中類的屬性可觀察集合WPF

<Window x:Class="PoSClientWPF.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid Margin="10"> 
     <ListBox x:Name="pumpListBox" ItemsSource="{Binding PumpCollection}" Grid.IsSharedSizeScope="True"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition SharedSizeGroup="ID" /> 
          <ColumnDefinition SharedSizeGroup="State" /> 
         </Grid.ColumnDefinitions> 
         <TextBlock Margin="2" Text="{Binding pumpID}" Grid.Column="0"/> 
         <TextBlock Margin="2" Text="{Binding state}" Grid.Column="1"/> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 
</Window> 

從研究其他職位關於這個非常錯誤。我已經包括增加this.DataContext = this;MainWindow以及具有:

public ObservableCollection<PumpItem> PumpCollection 
{ 
    get { return pumpCollection; } 
} 

爲了將ItemsSource綁定到它。我認爲我在XAML中聲明瞭綁定時出錯,但我不確定。我正在嘗試將屬性pumpIDstate添加到類實例的列表框中。

pumpItem如下所示:

public enum pumpState 
{ 
    Available, 
    customerWaiting, 
    Pumping, 
    customerPaying 
}; 

public enum fuelSelection 
{ 
    Petrol, 
    Diesel, 
    LPG, 
    Hydrogen, 
    None 
}; 

public class PumpItem 
{ 
    public string pumpID; 

    public double fuelPumped; 
    public double fuelCost; 

    public fuelSelection selection; 
    public pumpState state; 

    public PumpItem(string _ID) 
    { 
     this.pumpID = _ID; 
     this.fuelPumped = 0; 
     this.fuelCost = 0; 
     this.selection = fuelSelection.None; 
     this.state = pumpState.Available;    
    } 
} 

任何指針或幫助非常感謝。

回答

1

您無法綁定到字段。更改這些公共屬性

public class PumpItem 
{ 
    private string pumpID; 
    public string PumpId 
    { 
     get 
     { 
      return pumpId; 
     } 
     set 
     { 
      pumpId = value; 
     } 
    } 

    private double fuelPumped; 
    public double FuelPumped 
    { 
     get 
     { 
      return fuelPumped; 
     } 
     set 
     { 
      fuelPumped = value; 
     } 
    } 

    private double fuelCost; 
    public double FuelCost 
    { 
     get 
     { 
      return fuelCost; 
     } 
     set 
     { 
      fuelCost= value; 
     } 
    } 

    public fuelSelection selection; 
    public fuelSelection Selection 
    { 
     get 
     { 
      return selection; 
     } 
     set 
     { 
      selection = value; 
     } 
    } 

    public pumpState state; 
    public pumpState State 
    { 
     get 
     { 
      return state; 
     } 
     set 
     { 
      state = value; 
     } 
    } 

    public PumpItem(string _ID) 
    { 
     this.PumpID = _ID; 
     this.FuelPumped = 0; 
     this.FuelCost = 0; 
     this.Selection = fuelSelection.None; 
     this.State = pumpState.Available; 
    } 
} 

XAML

<TextBlock Margin="2" Text="{Binding PumpID}" Grid.Column="0"/> 
<TextBlock Margin="2" Text="{Binding State}" Grid.Column="1"/> 

檢查Output控制檯綁定錯誤

+0

正確的金錢有了答案,只是幾件事情像變量名稱大寫。例如。在你的例子中,它應該是私人字符串pumpId;以匹配你的變量名稱。也是pumpState狀態;應該可能是私人的。除此之外,更樂意標記爲已回答。謝謝 – James

相關問題