简体   繁体   中英

XAML Change the image source inside a Togglebutton

I'm trying to change the image source (in XAML only) when the Togglebutton "IsPressed" is true. But when iam trying to bind that property via the ''Image.Trigger'' it's not working.

This is what I have right now and the image is showing exactly like I want it to.

<ToggleButton x:Name="pbStations"
              Template="{StaticResource ContentOnlyTemplateToggleButton}">
  <StackPanel Orientation="Horizontal">
    <Image Source="/Sprites/Misc/ExpanderButtonClose.png"
           Margin="2"
           Stretch="Uniform"
           x:Name="img">
    </Image>
    <TextBlock Text="STATIONS"
               x:Name="tbStations"
               Style="{StaticResource tbTriggerGray}" />
  </StackPanel>
</ToggleButton>

This is what I have tried that doesn't work:

<ToggleButton x:Name="pbStations"
              Template="{StaticResource ContentOnlyTemplateToggleButton}">
  <StackPanel Orientation="Horizontal">
    <Image Source="/Sprites/Misc/ExpanderButtonClose.png"
           Margin="2"
           Stretch="Uniform"
           x:Name="img">
      <Image.Triggers>
        <DataTrigger Binding="{Binding ElementName=pbStations,Path=IsPressed}"
                     Value="True">
          <Setter TargetName="img"
                  Property="Image.Source"
                  Value="/Sprites/Misc/ExpanderButtonOpen.png" />
        </DataTrigger>
      </Image.Triggers>
    </Image>
    <TextBlock Text="STATIONS"
               x:Name="tbStations"
               Style="{StaticResource tbTriggerGray}" />
  </StackPanel>
</ToggleButton>

Try to set triggers for Image inside the style for that Image. Here is the code

<ToggleButton x:Name="pbStations"
              Template="{StaticResource ContentOnlyTemplateToggleButton}">
    <StackPanel Orientation="Horizontal">
        <Image Margin="2"
               Stretch="Uniform"
               x:Name="img">
        <Image.Style>
          <Style TargetType="Image">
            <Style.Triggers>                                  
              <DataTrigger 
                    Binding="{Binding ElementName=pbStations,Path=IsPressed}"
                    Value="True">        
                <Setter Property="Source"
                        Value="/Sprites/Misc/ExpanderButtonOpen.png" />   
              </DataTrigger>
              <DataTrigger 
                    Binding="{Binding ElementName=pbStations,Path=IsPressed}"
                    Value="False">        
                <Setter Property="Source"
                        Value="/Sprites/Misc/ExpanderButtonClose.png" />   
              </DataTrigger>
            </Style.Triggers>
      </Style>
     </Image.Style>
        </Image>
        <TextBlock Text="STATIONS"
                   x:Name="tbStations"
                   Style="{StaticResource tbTriggerGray}" />
    </StackPanel>
</ToggleButton>

I moved all your image-setting logic in 2 data triggers. The reason is for WPF engine will evaluate your Image's Source property after executing DataTrigger so DataTrigger's Setter will have lower priority than setting Source property for the image by hand.

Note that IsPressed only True when you hold your left mouse button down but when you release it IsPressed will became False. There is IsChecked property for ToggleButton class that can store state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM