[英]Binding Combobox Selection from StaticResouce to Datagrid ItemSource
[英]ItemSource Binding on ComboBox inside a DataGrid
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>
視圖模型(我為ItemModel創建了一個可觀察的集合,當我更新“文本列”的值並將值分配給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-兩者都不起作用。
嘗試這個:
<ComboBox ItemsSource="{Binding Path=DataContext.ComboBoxList, RelativeSource={RelativeSource AncestorType=DataGrid}}" BorderThickness="0" BorderBrush="Transparent" SelectedValue="{Binding Col1, Mode=TwoWay}"/>
默認情況下, ComboBox
的DataContext
是ItemModel
,因此您應該綁定到父DataGrid
的DataContext
的屬性。 您可以按照上述方法使用{RelativeSource}
進行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.