0

如何将 Image 的值绑定到 Image2 的值?

<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>
4

3 回答 3

8

由于Image2是你的一个属性ImageButton,你可以简单地将你的绑定源设置为ImageButton控件RelativeSource Self,并绑定到该Image2属性

<Setter Property="Image" 
        Value="{Binding Image2, RelativeSource={RelativeSource Self}}" />
于 2013-02-01T15:29:25.573 回答
0

您可以尝试Image1使用以下方式访问ElementName

{Binding ElementName=Image1, Path=DataContext}

确保您定义Image1

<Image Name="Image1" Width="30" Height="30" Source="{TemplateBinding Image}" />
于 2013-02-01T11:27:37.407 回答
-1

我不确定它是否适用于您的情况,但您可以尝试以下代码:

<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>
于 2013-02-01T11:44:56.630 回答