2010-02-18 96 views

回答

27

在Silverlight 4中,RelativeSource属性Binding还只支持“Self”和“TemplatedParent”,这个区域没有Silverlight 3的变化。

4

也许你可以实例化视图模型在XMAL为静态资源,然后引用,如您绑定的源。

<UserControl.Resources> 
    <vm:MainPageViewModel x:Key="ViewModel"/> 
</UserControl.Resources> 

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource ViewModel}}"> 
    <ListBox ItemsSource="{Binding Partitions}"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <toolkit:WrapPanel FlowDirection="LeftToRight" /> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Button Margin="10,0" Width="40" Content="{Binding}" Command="{Binding Source={StaticResource ViewModel}, Path=ButtonCommand}" CommandParameter="{Binding}"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

</Grid> 
16

RelativeSource AncestorTypeis supported in Silverlight 5,它现在可用。

<TextBlock Text="{Binding Name}" 
      FontSize="{Binding DataContext.CustomFontSize, 
       RelativeSource={RelativeSource AncestorType=UserControl}}" 
/> 
相关问题