David Horák
David Horák

Reputation: 5565

WPF , how set value as binding in trigger of same element value

How bind value of Image to Image2 value ?

<Style x:Key="ImageButton" TargetType="c:ImageButton">
       <Setter Property="Image" Value="/*My image 1*/" />
       <Setter Property="Image2" Value="/*My image 2*/" />
       <Setter Property="Template">
          <Setter.Value>
             <ControlTemplate TargetType="c:ImageButton">
                   <StackPanel Orientation="Horizontal" Height="30" Width="30">
                       <Image Width="30" Height="30" Source="{TemplateBinding Image}" />
                    </StackPanel>
             </ControlTemplate>
          </Setter.Value>
       </Setter>
        <Style.Triggers>
           <Trigger Property="IsMouseOver" Value="True">
               <Setter Property="Image" Value="{Binding to Property Image2 value}" /> // How bind value to Image2
           </Trigger>
       </Style.Triggers>

</Style>

Upvotes: 0

Views: 5065

Answers (3)

Rachel
Rachel

Reputation: 132628

Since Image2 is a property on your ImageButton, you can simply set your binding source to be the ImageButton control by using RelativeSource Self, and bind to the Image2 property

<Setter Property="Image" 
        Value="{Binding Image2, RelativeSource={RelativeSource Self}}" />

Upvotes: 8

Alex Butenko
Alex Butenko

Reputation: 3774

I am not sure if it will work in your case, but you can try this code:

<Style x:Key="ImageButton" TargetType="c:ImageButton">
    <Setter Property="Image" Value="/*My image 1*/" />
    <Setter Property="Image2" Value="/*My image 2*/" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="c:ImageButton">
                <StackPanel Orientation="Horizontal" Height="30" Width="30">
                    <Image x:Name="MyImage" Width="30" Height="30" Source="{TemplateBinding Image}" />
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="MyImage" Property="Source" Value="{TemplateBinding Image2}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>                
        </Setter.Value>
    </Setter>
</Style>

Upvotes: -1

Andrey Gordeev
Andrey Gordeev

Reputation: 32559

You could try to access Image1 by using ElementName:

{Binding ElementName=Image1, Path=DataContext}

Make sure you define Image1 as

<Image Name="Image1" Width="30" Height="30" Source="{TemplateBinding Image}" />

Upvotes: 0

Related Questions