Hi so I have a datagrid with two columns. It looks like below:
<DataGrid ItemsSource="{Binding MyCollection}">
<DataGrid.Columns>
<DataGridTextColumn Width="85*" Header="Team Name" Binding="{Binding TeamName}"/>
<DataGridTemplateColumn Header="Prefix">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.AllowedPrefixes}"
SelectedValue="{Binding Prefix}">
//SelectedValue="{Binding Prefix}" - this is not working i also tried SelectedItem
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
Like You can see i have a DataGridTemplateColumn with a ComboBox. I have binded ComboBox ItemsSource to a collection which holds some fixed values. Now I Want to Bind SelectedValue of a ComboBox to a Prefix property of MyClass(this is a DataContext of my DataGrid). But since I set ItemsSource of this ComboBox to other Collection this binding doesn't work i think that datacontext have changed. Below is a class which is a datacontext for a datagrid:
public class MyClass
{
public string TeamName{get;set;}
public string Prefix{get;set;}
}
// DataGrid.DataContext is ObservableCollection<MyClass>
So the AllowedPrefixes collection is shown correctly in ComboBox, but Prefix propert is not being updated. How should i make this SelectedValue Binding correctly ?
EDIT.
Please note that ComboBox ItemsSource is different collecetion than the one i want to update with SelectedValue
EDIT.
I think it doesn't work because I set ComboBox ItemsSource to a different collection. I tried like below with no success:
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.AllowedPrefixes}"
SelectedValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridTextColumn}}, Path=DataContext.Prefix, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">