2016-08-18 89 views
0

我想在ObservableCollection<string> Values上綁定我的DataGridComboBoxColumn。爲此,我寫了這個.xaml代碼:DataGrid DataGridComboBoxColumn ItemsSource綁定不起作用

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding FilterMng.FilterCollection}" CanUserAddRows="False" SelectionChanged="ComboBox_SelectionChanged"> 
    <DataGrid.Columns> 
     <DataGridComboBoxColumn Header="Wert" Width="*" SelectedValueBinding="{Binding SelectedValue, UpdateSourceTrigger=PropertyChanged}"> 
      <DataGridComboBoxColumn.ElementStyle> 
       <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}"> 
        <Setter Property="ItemsSource" Value="{Binding Values}"/> 
       </Style> 
      </DataGridComboBoxColumn.ElementStyle> 
      <DataGridComboBoxColumn.EditingElementStyle> 
       <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}"> 
        <Setter Property="ItemsSource" Value="{Binding Values}"/> 
       </Style> 
      </DataGridComboBoxColumn.EditingElementStyle> 
     </DataGridComboBoxColumn> 
    </DataGrid.Columns> 
</DataGrid> 

FilterMng.cs是經理級的Filter.cs。它包含ObservableCollection<Filter> FilterCollection{get;set;}和一些方法,如public void CreateFilter(){}。但是這種方法很有效。當我執行方法CreateFilter();DataGrid顯示了一個條目。

Filter.cs代碼:

public class Filter : INotifyPropertyChanged 
{ 
    #region Properties 

    ObservableCollection<string> Values { get; set; } 

    private string _selectedProperty; 
    public string SelectedProperty 
    { 
     get { return this._selectedProperty; } 
     set { this._selectedProperty = value; this.OnPropertyChanged("SelectedProperty"); } 
    } 

    private string _selectedValue; 
    public string SelectedValue 
    { 
     get { return this._selectedValue; } 
     set { this._selectedValue = value; this.OnPropertyChanged("SelectedValue"); } 
    } 

    #endregion Properties 

    #region c'tor 

    public Filter() 
    { 
     if (this.Values == null) 
      this.Values = new ObservableCollection<string>(); 

     Values.Add("Entry1"); 
    } 

    #endregion c'tor 

    #region OnPropertyChanged 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    #endregion OnPropertyChanged 
} 

我可以對所有propertys綁定除了Values

現在我用Snoop檢查是否有任何ItemsSource錯誤。存在錯誤:

System.Windows.Data Error: 40 : BindingExpression path error: 'Values' property not found on 'object' ''Filter' (HashCode=31703865)'. BindingExpression:Path=Values; DataItem='Filter' (HashCode=31703865); target element is 'TextBlockComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') 

有沒有人有想法?

回答

1
ObservableCollection<string> Values { get; set; } 

在此setter屬性,你不通知其屬性更改

嘗試是這樣的

private ObservableCollection<string> values; 
public ObservableCollection<string> Values { get { return values; } set {values = value; this.OnPropertyChanged("Values ");} } 

我相信這是你的代碼問題

+0

編輯的回答 –

+0

非常感謝!現在它可以工作。 – MyNewName

相關問題