C# Silverlight绑定到父数据模板
我有一组用户和一组角色。 对于每个用户,我将呈现角色列表。 我想将复选框绑定到某个用户属性 这是我的代码(第38行): 为什么选择这一行:C# Silverlight绑定到父数据模板,c#,silverlight,xaml,data-binding,binding,C#,Silverlight,Xaml,Data Binding,Binding,我有一组用户和一组角色。 对于每个用户,我将呈现角色列表。 我想将复选框绑定到某个用户属性 这是我的代码(第38行): 为什么选择这一行: {Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid},Path=DataContext} …绑定到角色类,而不是用户类 绑定应该在第21行找到网格,它的DataContext类型是User,不是吗?您可以使用“ancestorLevel”将级别上移一级吗 {
{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid},Path=DataContext}
…绑定到角色类,而不是用户类
绑定应该在第21行找到网格,它的DataContext类型是User,不是吗?您可以使用“ancestorLevel”将级别上移一级吗 {Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid},AncestorLevel=1,Path=DataContext} 奥利弗还有另一种方法:- 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>
这样修改了我的代码。在我的转换器中仍然有Rolewell类型的值。它起作用了。非常感谢。关于第二次猜测-我只需要整个DataContext,所以我不需要指定PropertyNameYes在这种情况下它应该是DataContext而不是IsChecked我猜不,我有一个复杂的转换器,需要所有用户类作为输入来确定是否应该选中复选框。Nvm,现在没关系了。你的代码工作起来很有魅力
IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid}, Path=DataContext.PopertyName, Converter={StaticResource CheckBoxToSelectedArrayConverter}, Mode=TwoWay}" Content="{Binding RoleName}" Tag="{Binding RoleId}"