Silverlight 4 RelativeSource FindAncestor绑定

时间:2010-02-18 18:55:00

标签: .net silverlight binding relativesource

Silverlight 4中是否会有RelativeSource FindAncestor,AncestorType?

4 个答案:

答案 0 :(得分:27)

在Silverlight 4中,RelativeSource的{​​{1}}属性仍然只支持“Self”和“TemplatedParent”,此区域的Silverlight 3没有变化。

答案 1 :(得分:16)

RelativeSource AncestorType is supported in Silverlight 5,现已上市。

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

答案 2 :(得分:4)

也许您可以将XMAL中的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>

答案 3 :(得分:3)