I want to bind List
(which contains data from LINQ query) with DataGridComboBoxColumn
as ItemsSource
. SelectedValue
property is taken from other List
with data for whole 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;
}
}
Here are a lot of examples of DataGridComboBoxColumn
Binding but none of these can help to solve my issue.
It looks like you are asking for a property on your DataContext called MyValueItem.
<DataGridComboBoxColumn ItemsSource="{Binding MyValueItem}" SelectedItemBinding="{Binding Source="{StaticResource myvalue}" Header="Values" Width="Auto"/>
But you are passing the value of MyValueItem as the DataContext.
DG1.DataContext = MyValueItem;
You need to set the DataContext to an item with a "MyValueItem" property. Replace the above line with the one below.
DG1.DataContext = this;
Where "this" is the MyWindow which has the MyValueItem property.
...
Alternatively, you could change the binding to the below, which will take whatever item you pass to it as the binding source.
<DataGridComboBoxColumn ItemsSource="{Binding}" SelectedItemBinding="{Binding Source="{StaticResource myvalue}" Header="Values" Width="Auto"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.