在财产安置中与祖先结盟

时间:2013-08-06 14:31:39

标签: wpf binding

我控制内容" MyControl"这有一个属性" GlobalBackground"。 对于我有这样风格的物品。

<ControlTemplate x:Key="XTemplate" TargetType="{x:Type local:MyControlItem}">
    <Grid HorizontalAlignment="Stretch">
        <Rectangle Height="2" Fill="{Binding GlobalBackground, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:MyControl}}"/>
            ...
    </Grid>
</ControlTemplate>
<Style x:Key="XStyle" TargetType="{x:Type local:MyControlItem}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="Template" Value="{StaticResource XTemplate}"/>
</Style>

这可以按预期工作。

现在我想使用绑定到Itemscontrol的不同属性的相同模板。 所以想法是通过setter设置属性(Style Triggers) 当我这样做时

<ControlTemplate x:Key="XTemplate" TargetType="{x:Type local:MyControlItem}">
    <Grid HorizontalAlignment="Stretch">
        <Rectangle Height="2" Fill="{TemplateBinding Background}"/>
            ...
    </Grid>
</ControlTemplate>
<Style x:Key="XStyle" TargetType="{x:Type local:MyControlItem}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="Background" Value="{Binding GlobalBackground, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:MyControl}}" />
    <Setter Property="Template" Value="{StaticResource XTemplate}"/>
</Style>

绑定失败。

没有办法在样式设置器中使用FindAncestor绑定,或者我只是做错了什么?

曼弗雷德

1 个答案:

答案 0 :(得分:0)

您似乎在声明AncestorType时出错...请参阅此示例:

<Button Foreground="Blue" Background="Red">
    <Rectangle Margin="20" Width="50" Height="50">
        <Rectangle.Style>
            <Style>
                <Setter Property="Rectangle.Fill" Value="{Binding Foreground, 
                    RelativeSource={RelativeSource Mode=FindAncestor, 
                    AncestorType={x:Type Button}}}" />
            </Style>
        </Rectangle.Style>
    </Rectangle>
</Button>
相关问题