DataGrid 内的组合框未填充列表。
我认为 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>
视图模型(我已经为 ItemModel 创建了一个可观察的集合,当我更新文本列的值并将值分配给模型对象时,它工作正常):
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 - 两者都不起作用。
尝试这个:
<ComboBox ItemsSource="{Binding Path=DataContext.ComboBoxList, RelativeSource={RelativeSource AncestorType=DataGrid}}" BorderThickness="0" BorderBrush="Transparent" SelectedValue="{Binding Col1, Mode=TwoWay}"/>
The DataContext
of the ComboBox
is the ItemModel
默认情况下,所以你应该绑定到DataContext
父母的DataGrid
。你可以使用{RelativeSource}
如上所述。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)