views:

531

answers:

1

I have created an attached property to extend a Button class with additional state:

<Button v:ExtensionHelper.OperationMode="{Binding MyObject.OperationMode}" Command="{Binding MyObject.Select}" Style="{StaticResource operationModeControlTemplateStyle}" />

Then I want to acces this value in the controltemplate using Datatriggers like this:

<Style x:Key="operationModeControlTemplateStyle" TargetType="Button">
    <Setter Property="IsHitTestVisible" Value="true" />        
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <Image x:Name="hand" Source="hand.png" />
                    <Image x:Name="cross" Source="cross.png" />
                </Grid>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=(v:ExtensionHelper.OperationMode), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" Value="Manual">
                        <Setter TargetName="cross" Property="Visibility" Value="Collapsed" />
                        <Setter TargetName="hand" Property="Visibility" Value="Visible" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=(v:ExtensionHelper.OperationMode), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" Value="Disabled">
                        <Setter TargetName="cross" Property="Visibility" Value="Visible" />
                        <Setter TargetName="hand" Property="Visibility" Value="Collapsed" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=(v:ExtensionHelper.OperationMode), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" Value="Automatic">
                        <Setter TargetName="cross" Property="Visibility" Value="Collapsed" />
                        <Setter TargetName="hand" Property="Visibility" Value="Collapsed" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

But I get this error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.Button', AncestorLevel='1''. BindingExpression:Path=(0); DataItem=null; target element is 'Button' (Name=''); target property is 'NoTarget' (type 'Object')

The binding expression in the datagrigger seems to be incorrect. What am I doing wrong?

+1  A: 

Hello bitbonk,

the answer is pretty simple and if you would have spent your time reading the docs instead of wasting it posting a question on stackoverflow you would already know:

Binding="{Binding RelativeSource={RelativeSource Self}, Path=(v:ExtensionHelper.OperationMode)}"
bitbonk
bitbonk answering bitbonk with some sarcasm thrown in. ;) Now accept the answer too!! Btw, you could have deleted this question if you found the answer after "reading the docs".
Yogesh
I can only accept my answer after two days. I would prefer to leave it here. Who known, it might help others (how to resolve the same binding problem or to keep them from asking questions too quickly).
bitbonk
-1 for tone of voice - I found this answer unpleasant to read
Ray Burns
@Ray Burns: you did notice that I answered my own question and that I was trying to be funny ? :)
bitbonk