fattikus
fattikus

Reputation: 542

Trigger on an Inner/Attached property

Trigger on an Inner property

<Button BorderBrush="Black" BorderThickness="2" x:Name="TimeButton" ClickMode="Press" Click="SetTime_Click" Height="26" HorizontalAlignment="Left" Margin="15, 0, 0, 0" Style="{StaticResource ImageButtonStyle}" ToolTip="Set Time" Width="26">
    <Button.Background>
        <ImageBrush x:Name="TimeImageBrush" ImageSource="/YCS;component/Images/Clock.png" Stretch="Uniform" TileMode="None" />                                        
    </Button.Background>  
</Button>

I need to make a trigger to set the ImageBrush in the Button.Background property to something different according to a boolean named HasHours which I can bind easily from my itemssource, any one knows how I can achieve this, I could not find any examples linking to this property....

I tried something like this

<Button.Triggers>
    <DataTrigger Binding="{Binding HasHours}" Value="false">
        <Setter TargetName="TimeImageBrush" Property="ImageSource" Value="/YCS;component/Images/ClockRed.png"/>
    </DataTrigger>
</Button.Triggers>

but it gives me this error:

Cannot find the static member 'ImageSourceProperty' on the type 'ContentPresenter'.

Any help is much appreciated

Upvotes: 0

Views: 589

Answers (2)

Clemens
Clemens

Reputation: 128061

This is perhaps not exactly an answer to your question.

First, i guess you won't be able to add a DataTrigger to the Triggers collection, since that only supports EventTriggers.

But, you could define the DataTrigger in the Button's Style. Here, instead of setting the ImageBrush's ImageSource property, simply set a new ImageBrush as Background.

<Button ...>
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasHours}" Value="False">
                    <Setter Property="Background">
                        <Setter.Value>
                            <ImageBrush ImageSource="/YCS;component/Images/ClockRed.png"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>                    
        </Style>
    </Button.Style>
</Button>

Upvotes: 1

GameAlchemist
GameAlchemist

Reputation: 19294

  1. Put the image as Content, not as Background, since you have no content.
  2. Put the DataTrigger in the Triggers of the Image, not of the Button. You will have to seek for the DataContext of the Trigger :
    So something like :

    <Button ... >  
      <Image ... >  
        <Image.Triggers>  
           <DataTrigger   
               Binding="{Binding Path= HasHours, RelativeSource={RelativeSource FindAncestor,  
                        AncestorType={x:Type Button}}}"   
                        Value="false" >  
               <Setter Property="ImageSource" Value="/YCS;component/Images/ClockRed.png"/>
          </DataTrigger>
        </Image.Triggers>
       </Image>
      </Button> 
    

Upvotes: 0

Related Questions