views:

2894

answers:

4

Hello,

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

A: 

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
+2  A: 

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

Joseph Melettukunnel
A: 

Niiicce, thx...

This did helped me indeed!

SlickRick
A: 

Can you tell me what i must do in if my <DataTemplate> is in the embedded resource file ?

Alex