![](/img/trans.png)
[英]DataGridComboBoxColumn binding to a different list that the DataGrid ItemsSource
[英]DataGrid DataGridComboBoxColumn ItemsSource Binding not working
我想將我的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')
有一個想法嗎?
ObservableCollection<string> Values { get; set; }
在此setter屬性中,您沒有通知要更改哪個屬性
試試這個
private ObservableCollection<string> values;
public ObservableCollection<string> Values { get { return values; } set {values = value; this.OnPropertyChanged("Values ");} }
我確定這是您的代碼存在的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.