I have this XAML
<DataGrid ItemsSource="{Binding Users}"
CanUserAddRows="False"
SelectedItem="{Binding SelectedUser}"
AutoGenerateColumns="False"
Grid.Column="0">
<DataGrid.Columns>
<DataGridTextColumn Header="First name" Binding ="{Binding FirstName}"/>
<DataGridTextColumn Header="Last name" Binding ="{Binding LastName}"/>
<DataGridComboBoxColumn Width="*" Header="Catégorie"
SelectedItemBinding="{Binding Role}">
<DataGridComboBoxColumn.ElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource"
Value="{Binding Path=DataContext.ComboBox_Roles,
RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type UserControl}}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding
Path=DataContext.ComboBox_Roles, RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type UserControl}}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
I want combobox to bind to a my roles collection
My model
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Role { get; set; }
}
And in my view model:
public ManageUsersViewModel()
{
_users = new ObservableCollection<User>
{
new User { FirstName = "First1", LastName = "Last1", Role = "Admin" },
new User { FirstName = "Last2", LastName = "Last2", Role ="User" },
};
ComboBox_Roles = new ObservableCollection<string> { "ABC", "BCD", "ASDSAD" };
}
public ObservableCollection<User> Users
{
get => _users;
set
{
_users = value;
}
}
public ObservableCollection<string> ComboBox_Roles { get; set; }
How can i bind this items? Seems like the problem is that i can't bind that way to a collection inside a collection. The thing i want to implement that is i have a combobox with a role list in it, and i can change my user's role by selecting one of the values of combobox. Upd: updated code because i realised i was doing.