DataGrid中的ComboBox没有填充List。 我认为ItemSource Path存在一个问题:
查看(DataGrid的xaml代码):
<DataGrid CanUserAddRows="True" ItemsSource="{Binding Path=GridCollection, Mode=TwoWay}" AutoGenerateColumns="False" IsReadOnly="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Column 1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.ComboBoxList}" BorderThickness="0" BorderBrush="Transparent" SelectedValue="{Binding Col1, Mode=TwoWay}"/>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Column 2" Width="170" Binding="{Binding Col2, Mode=TwoWay}"/>
</DataGrid>
View Model(我已经为ItemModel创建了一个可观察的集合,当我更新Text Column的值并将值赋给Model对象时,这个工作正常):
public ObservableCollection<ItemModel> GridCollection
{
get
{
return this.gridCollection;
}
set
{
this.gridCollection = value;
base.RaisedPropertyChanged("GridCollection");
}
}
public List<string> ComboBoxList
{
get
{
return this.comboBoxList;
}
set
{
this.comboBoxList = value;
base.RaisedPropertyChanged("GridList");
}
}
public MultiValueViewModel(string data)
{
this.GridCollection = new ObservableCollection<ItemModel>();
this.GridCollection.Add(new ItemModel("ABC", 0));
this.ComboBoxList = new List<string>();
//Add items to list
}
模型(模型包含一个具有2个属性的类):
public class ItemModel
{
public ItemModel(string col1, double col2)
{
this.Col1 = col1;
this.Col2 = col2;
}
public string Col1 { get; set; }
public double Col2 { get; set; }
}
我尝试使用Path = ComboBoxList和DataContext.ComboBoxList - 这两项都不起作用。
答案 0 :(得分:1)
试试这个:
<ComboBox ItemsSource="{Binding Path=DataContext.ComboBoxList, RelativeSource={RelativeSource AncestorType=DataGrid}}" BorderThickness="0" BorderBrush="Transparent" SelectedValue="{Binding Col1, Mode=TwoWay}"/>
DataContext
的{{1}}默认为ComboBox
,因此您应该绑定到父ItemModel
的{{1}}的属性。您可以按照上面的DataContext
执行此操作。