I have Array of Users and array of Roles. For each user i'am rendering list of roles. I want to bind checkboxes to some user property.
Here is my code ( line 38 ): https://gist.github.com/sadgb/30e2b75f2fff159bc26e#file-gistfile1-xml-L38
Why this line:
{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid}, Path=DataContext}
...binds to Role class, not to User class?
Binding should find grid at line 21, which has DataContext of type User isn't it?
There is another way: - 1. Give any name to your grid. 2. then do ur binding like this :
DataContext="{Binding ElementName=Gridname,Path=DataContext}"
Here is your edited code:
<DataTemplate DataType="usersService:User">
<Grid Name="TheGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding UserId}"/>
<telerik:RadListBox Grid.Row="1"
ItemsSource= "{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=users:UsersPage}, Path=RolesViewModel.Roles}" IsTextSearchEnabled="false">
<telerik:RadListBox.ItemTemplate>
<DataTemplate DataType="accessControlSubsystem:Role">
<StackPanel>
<CheckBox
IsChecked="{Binding ElementName=TheGrid,Path=DataContext , Converter={StaticResource CheckBoxToSelectedArrayConverter}, Mode=TwoWay}" Content="{Binding RoleName}" Tag="{Binding RoleId}" />
</StackPanel>
</DataTemplate>
</telerik:RadListBox.ItemTemplate>
</telerik:RadListBox>
</Grid>
</DataTemplate>
I guess this will solve your problem or at least give you a good start.
One more thing I noticed in your code is that you didn't specify the property name in your relative binding:
IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid}, Path=DataContext.PopertyName, Converter={StaticResource CheckBoxToSelectedArrayConverter}, Mode=TwoWay}" Content="{Binding RoleName}" Tag="{Binding RoleId}"
Can you move the level up one using "ancestorLevel"
{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid}, AncestorLevel=1, Path=DataContext}
Oliver
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.