13

I'd like to do following:

public List<Users> PreLoadedUserList { get; set; }
public List<RowEntries> SomeDataRowList { get; set; }

public class Users
{
    public int Age { get; set; }
    public string Name { get; set; }
}
public class SomeDataRowList 
{
    public int UserAge { get; set;
}

Now my (WPF Toolkit) DataGrid looks like this:

<my:DataGrid AutoGenerateColumns="False" MinHeight="200" 
             ItemsSource="{Binding Path=SomeDataRowList}">
    <my:DataGridComboBoxColumn Header="Age" 
                               ItemsSource="{Binding Path=PreLoadedUserList}" 
                               DisplayMemberPath="Name" 
                               SelectedValueBinding="{Binding Path=UserAge}"/>

</my:DataGrid>

Now my problem is, that PreLoadedUserList is outside of the ItemSource (SomeDataRowList) and I don't know how to bind to something outside of it. What I actually want it: - Display in the ComboBox PreLoadedUserList - Set the Value of (RowEntries) SelectedItem.UserAge to the Value of the selected ComboboxItem.Age

Let me know if my explanation is too weird :-)

Thank you, Cheers

Joseph jun. Melettukunnel
  • 6,267
  • 20
  • 69
  • 90

2 Answers2

24

Here we go :-)

<my:DataGridTemplateColumn Header="SomeHeader">
    <my:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox SelectedValuePath="UserAge" 
                SelectedValue="{Binding Age}" 
                DisplayMemberPath="Name" 
                ItemsSource="{Binding Path=DataContext.PreLoadedUserList, 
                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" 
                IsReadOnly="True" Background="White" />
        </DataTemplate>
    </my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>

Hope this can help someone else too.

Cheers

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
Joseph jun. Melettukunnel
  • 6,267
  • 20
  • 69
  • 90
  • Wow, I was trying and trying with a 'DataGridComboBoxColumn' and nothing .. But then MAGIC ! Your beautiful example works (with Template stuff) Thanks ! – Philippe Lavoie Feb 15 '11 at 16:43
  • This helped me solve my question, Joseph. Handling these header combobox's was my next task. Your solution helped me a lot and saved my lots of time. Thanks a lot. – Tvd Aug 30 '13 at 09:18
0

If RowEntries is a custom class, just give it a reference to the PreLoadedUserList. Then, each instance has a pointer to it and you can use it in your binding.

Just a suggestion, class names like Users and RowEntries suggest that they are collections but your usage looks like they're the item not the collection. I'd use singular names to avoid any confusion. I'd do something like this

public List<User> PreLoadedUserList { get; set; }
public List<RowEntry> SomeDataRowList { get; set; }

public class User
{
    public int Age { get; set; }
    public string Name { get; set; }
}
public class RowEntry 
{
    public int UserAge { get; set; }
    public List<User> PreLoadedUserList { get; set; }
}

// at the point where both PreLoadedUserList is instantiated
// and SomeDataRowList is populated
SomeDataRowList.ForEach(row => row.PreLoadedUserList = PreLoadedUserList);
Rich
  • 36,270
  • 31
  • 115
  • 154