I am pretty new to WPF and I am stuck on this one, I couldn't find anything that helped me solve my problem.
I am trying to create DataGrid where I would like to compare multpiple people from imported file to people in database. My idea of that was creating told DataGrid (of person data) with ComboboxColumn (with list of people from database). My main problem is that both people to compare and to fill combobox, may differ because of user choice in previous window. Having some database work I save both choices in Window_Loaded as:
personList //list of Person objects to populate combobox
pisList //list of Person objects to fill DataGrid
Now I have no idea how to get both those values to DataGrid so I populate ComboboxColumn as well. I tried do get one of those Values as DataContext od whole Window and second as datagrid ItemsSource.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Database db = new Database();
List<Person> personList = (...);
List<Person> pislist = (...);
dgPlayers.ItemsSource = impTeam.PlaysInSquad.ToList();
wPlayers.DataContext = personList;
}
As I failed miserably to get those 2 in one DataGrid, I created 2 separate, but still can make it work.
<DataGrid Name="dgPlayers" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Number" Binding="{Binding Number}" IsReadOnly="True"/>
<DataGridTextColumn Header="Name" Binding="{Binding Person.Name}" IsReadOnly="True"/>
<DataGridTextColumn Header="Surname" Binding="{Binding Person.Surname}" IsReadOnly="True"/>
<DataGridTextColumn Header="Role" Binding="{Binding IDRole}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid Name="dgList" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn x:Name="List" ItemsSource="{Binding DataContext.Person}" DisplayMemberPath="Name"/>
</DataGrid.Columns>
</DataGrid>
First DataGrid is ok, but still, I can't properly populate ComboboxColumn + I would love to see that in one DataGrid instead of two.
EDIT
Even though I already tried it before without success, solution from this thread worked for me after like 3rd try: How to Bind data to DataGridComboBoxColumn in DataGrid using MVVM
DataGridColumns
does not lies in same visual tree as that ofDataGrid
, so it doesn't inheritDataContext
from its parent. That's why you are not able to bind it using simple binding syntax. However, there are many workarounds to achieve that.I'll list two of them here:
First take advantage of
Freezable
class. Magic of Freezable objects is that it can inherit the DataContext even when they’re not in the visual or logical tree. You can read more about here Bind Data when DataContext is not inherited.Add this class in your project:
XAML usage:
Second, if you are using WPF 4.0, you can take adavantage of x:Reference which allows you to bind to element name even if it does not exist in Visual tree.
Create TextBlock whose Visibility will be Collapsed and set its DataContext to personsList.
and in code behind
Now you can bind in DataGrid like this: