还有另一种方法: - 1. 为您的网格命名。2.然后像这样做你的绑定:
DataContext="{Binding ElementName=Gridname,Path=DataContext}"
这是您编辑的代码:
<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>
我想这会解决你的问题,或者至少给你一个好的开始。
我在您的代码中注意到的另一件事是您没有在相对绑定中指定属性名称:
IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid}, Path=DataContext.PopertyName, Converter={StaticResource CheckBoxToSelectedArrayConverter}, Mode=TwoWay}" Content="{Binding RoleName}" Tag="{Binding RoleId}"