我想将List
(包含来自 LINQ 查询的数据)与DataGridComboBoxColumn
as绑定ItemsSource
。SelectedValue
属性是从其他List
数据中获取的整体DataGrid
。
XAML:
<DataGrid x:Name="DG1" x:Uid="DG1" AutoGenerateColumns="False" AlternationCount="2" SelectionMode="Single" Margin="0,5,0,0" HorizontalAlignment="Stretch">
<DataGrid.Columns>
//...
<DataGridComboBoxColumn ItemsSource="{Binding MyValueItem}" SelectedItemBinding="{Binding Source="{StaticResource myvalue}" Header="Values" Width="Auto"/>
C#:
public class ValuesInfo
{
public int id { get; set; }
public string number { get; set; }
public string myvalue { get; set; }
}
public class MyValueItems : List<string>
{
}
public partial class MyWindow : Window
{
public MyValueItems MyValueItem { get; set; }
public MyWindow()
{
InitializeComponent();
}
private void MyWindow_Load(object sender, RoutedEventArgs e)
{
MyValueItem = new MyValueItems();
sqlDataContext dc = new sqlDataContext();
var allValueNames = (from p in dc.Names
orderby p.id ascending
select p.name);
MyValueItem.AddRange(allValueNames);
DG1.DataContext = MyValueItem;
}
public void LoadValues()
{
List<ValuesInfo> ValueList = new List<ValuesInfo> { };
//...
for (int i = 1; i <= (int)rdr[0]; i++)
{
var dbset = (from p in dc.Values
where p.id == i
orderby p.id ascending
select p).Single();
var ValueName = (from p in dc.Names
where p.id == dbset.valueId
select p.name).Single();
ValuesInfo valueItem = new ValuesInfo
{
id = dbset.id,
number = dbset.number,
myvalue = valueName
};
ValueList.Add(valueItem);
}
DG1.ItemsSource = ValueList;
}
}
这里有很多DataGridComboBoxColumn
Binding 的例子,但这些都不能帮助解决我的问题。