2009-12-22 117 views

回答

5

理想的情况下,将其设置使得bindee元素总是有一个ScaleTransform,但它是标识转换。然后命名该ScaleTransform,并使用通常的元件到元件语法绑定到它:

<TextBlock Text="One"> 
    <TextBlock.LayoutTransform> 
    <ScaleTransform ScaleX="1" 
        ScaleY="1" 
        x:Name="s" /> <!-- Note the x:Name --> 
    </TextBlock.LayoutTransform> 
</TextBlock> 

<TextBlock Text="Two"> 
    <TextBlock.LayoutTransform> 
    <ScaleTransform ScaleX="{Binding ScaleX, ElementName=s}" 
        ScaleY="{Binding ScaleY, ElementName=s}" /> 
    </TextBlock.LayoutTransform> 
</TextBlock> 

如果需要绑定到元件具体地,可以通过属性例如向下遍历{Binding LayoutTransform.ScaleX, ElementName=someElement}。如果源元素没有LayoutTransform或者它不是ScaleTransform,它将不会运行,但会报告运行时绑定错误。

+0

好的,谢谢!所以为了让事情更复杂一点,我需要绑定到textblocks模板父...所以我需要使用templateBinding,仍然适用于这种情况? – Mark 2009-12-22 22:36:00

+1

我这么认为,但您可能需要使用'{RelativeSource TemplatedParent}'而不是'{TemplateBinding}',而且我非常确定您需要使用LayoutTransform.ScaleX方法而不是ElementName方法(因为您可以没有RelativeSource和ElementName)。因此,'{Binding LayoutTransform.ScaleX,RelativeSource = {RelativeSource TemplatedParent}}'。顺便说一下,需要考虑的一些事情:您是否特别想绑定一个比例尺,或者只是整个变换'TextBlock LayoutTransform =“{TemplateBinding LayoutTransform}”'? – itowlson 2009-12-22 22:44:38

+0

好问题,我认为规模应该足够......生病让你知道。我尝试了你刚刚建议的,但是我得到了绑定错误:路径错误:'对象''MatrixTransform'上找不到'ScaleX'属性。所以我改变了我的绑定表达式为:但这并没有做任何事情或给错误:( – Mark 2009-12-22 22:50:31

相关问题