-4

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}">
MajkeloDev
  • 1,661
  • 13
  • 30
  • 1
    please add the error or a description how it is not working – Breeze Jul 16 '15 at 19:19
  • You probably want to use `SelectedItem`, not `SelectedValue` - http://stackoverflow.com/questions/4902039/difference-between-selecteditem-selectedvalue-and-selectedvaluepath – Michael Jul 16 '15 at 20:25
  • There is no error it's just not updating the viemodels prefix property. Selected Item won't change anything. – MajkeloDev Jul 16 '15 at 20:52
  • Go on DownVoters - keep DownVoting instead of help. Not even explain ... – MajkeloDev Jul 16 '15 at 20:55
  • Add `Mode=TwoWay` to the Binding, it might be `OneWay` by default – Breeze Jul 16 '15 at 21:56
  • In addition to that: I don't know if this is important, but there is a type mismatch: you mentioned int an earlier version that your `ItemSource` contains `Integers`, but `Prefix` is a `string` – Breeze Jul 16 '15 at 21:58
  • @Dave Thanks for suggestions, TwoWay Binding didn't help, checking Prefix Type to int also ... – MajkeloDev Jul 17 '15 at 06:28

2 Answers2

0

Try the following for your ComboBox:

<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.AllowedPrefixes}" 
          SelectedValue="{Binding DataContext.Prefix, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, UpdateSourceTrigger=PropertyChanged}">

If your DataGrid is inside a UserControl and not a Windows change the AncestorType to UserControl

Tomtom
  • 9,087
  • 7
  • 52
  • 95
  • I think You missunderstood me. My Datagrid itemssource is ObservableCollection where MyClass containt property prefix. So my user control DataContext doesn't have property prefix – MajkeloDev Jul 17 '15 at 07:03
0

Ok so despite of all You - People who didn't know the answer and DownVoted me without explantaion. I Found a solution. I'm posting it below someone may use it in the future, I had to set NotifyOnTargetUpdated to true:

<ComboBox 
    SelectedItem="{Binding Prefix, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"
    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.AllowedPrefixes}">
MajkeloDev
  • 1,661
  • 13
  • 30