.net Silverlight 4 RelativeSource FindAncestor 绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2291310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:56:24  来源:igfitidea点击:

Silverlight 4 RelativeSource FindAncestor binding

.netsilverlightbindingrelativesource

提问by David

Will there be RelativeSource FindAncestor, AncestorType... in Silverlight 4?

Silverlight 4 中是否会有RelativeSource FindAncestor、AncestorType...?

回答by AnthonyWJones

In Silverlight 4 the RelativeSourceproperty of Bindingstill only supports "Self" and "TemplatedParent", there is no change from Silverlight 3 in this area.

在 Silverlight 4 中的RelativeSource属性Binding仍然只支持“Self”和“TemplatedParent”,这方面与 Silverlight 3 没有变化。

回答by Drew Noakes

RelativeSource AncestorTypeis supported in Silverlight 5, which is available now.

RelativeSource AncestorTypeSilverlight 5 支持,现在可用。

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

回答by Ralph Shillington

Perhaps you could instantiate the ViewModel in the XMAL as a static resource then reference that as the source in your binding.

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