【发布时间】:2016-08-18 06:13:05
【问题描述】:
我想将我的DataGridComboBoxColumn 绑定到ObservableCollection<string> Values。为此我写了这个.xaml代码:
<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
}
我可以绑定除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')
你们有什么想法吗?
【问题讨论】: