Silverlight 4 相对源(RelativeSource)FindAncestor绑定

21

在Silverlight 4中是否会有RelativeSource FindAncestor、AncestorType等功能?

4个回答

27
在 Silverlight 4 中,BindingRelativeSource 属性仍然只支持 "Self" 和 "TemplatedParent",与 Silverlight 3 相比在这个领域没有改变。

16

RelativeSource AncestorType在现在已经可以使用了,它在Silverlight 5中得到了支持

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

4
也许您可以在XAML中将ViewModel实例化为静态资源,然后在绑定中引用该资源作为源。
<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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接