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')
有沒有人有想法?
編輯的回答 –
非常感謝!現在它可以工作。 – MyNewName