How to set Image Source property by Xaml Behaviors? - xaml

I want change background image on every FlipView item.
I'm try this code, but it fired error Only IBehavior types are supported in a BehaviorCollection.
How set image source correctly?
<Grid>
<i:Interaction.Behaviors>
<core:DataTriggerBehavior
Binding="{Binding SelectedIndex, ElementName=TourFlipView}"
ComparisonCondition="Equal"
Value="1" />
<core:ChangePropertyAction
TargetObject="{Binding ElementName=TourFlipViewBackgroundImage}"
PropertyName="Source"
Value="ms-appx:///Assets/Images/2.png" />
</i:Interaction.Behaviors>
<Image
x:Name="TourFlipViewBackgroundImage"
Source="ms-appx:///Assets/Images/1.png" />
<FlipView
x:Name="TourFlipView">
...
<FlipView/>
</Grid>

You did almost correct with small mistake. put your ChangePropertyAction inside DataTriggerBehavior
<i:Interaction.Behaviors>
<core:DataTriggerBehavior
Binding="{Binding SelectedIndex, ElementName=TourFlipView}"
ComparisonCondition="Equal"
Value="1" >
<core:ChangePropertyAction
TargetObject="{Binding ElementName=TourFlipViewBackgroundImage}"
PropertyName="Source"
Value="ms-appx:///Assets/Images/2.png" />
</core:DataTriggerBehavior>
</i:Interaction.Behaviors>

Related

How to apply data triggers on image based on entry

I have one entry and a image, as initial load of page i want to show close.png image, after entry is focus or user type anything in entry i want to show searchingicon.png image.
<Entry x:Name="MainSearchPage"/>
<Image Grid.Row="0" Source="close.png" Style="{DynamicResource EntryIcons}" Grid.Column="1" HeightRequest="16" WidthRequest="15" Margin="0,0,0,0" Aspect="AspectFit">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ClearCommand}"/>
</Image.GestureRecognizers>
<Image.Triggers>
<DataTrigger TargetType="Image" Binding="{Binding Source={x:Reference MainSearchPage},Path=Text.Length}" Value="0">
<Setter Property="Source" Value="searchingicon.png" />
</DataTrigger>
</Image.Triggers>
</Image>
You can try to use the following code to do that.
<Image Grid.Row="0" Source="searchingicon.png" Style="{DynamicResource EntryIcons}" Grid.Column="1" HeightRequest="16" WidthRequest="15" Margin="0,0,0,0" Aspect="AspectFit">
<Image.Triggers>
<MultiTrigger TargetType="Image">
<MultiTrigger.Conditions>
<BindingCondition Binding="{Binding Source={x:Reference MainSearchPage}, Path=IsFocused}" Value="false"/>
<BindingCondition Binding="{Binding Source={x:Reference MainSearchPage}, Path=Text}" Value="{x:Null}"/>
</MultiTrigger.Conditions>
<Setter Property="Source" Value="close.png" />
</MultiTrigger>
<MultiTrigger TargetType="Image">
<MultiTrigger.Conditions>
<BindingCondition Binding="{Binding Source={x:Reference MainSearchPage}, Path=IsFocused}" Value="false"/>
<BindingCondition Binding="{Binding Source={x:Reference MainSearchPage}, Path=Text.Length}" Value="0"/>
</MultiTrigger.Conditions>
<Setter Property="Source" Value="close.png" />
</MultiTrigger>
</Image.Triggers>
</Image>
You just want to show close.png in two conditions:
The entry is unfocused and the entry's text length is 0, this is what the second <MultiTrigger> does.
The page is initial load (means the entry's text is null) and the entry is unfocused, this is what the first <MultiTrigger> does.

ToggleButton EventTriggerBehavior- binding image to background of togglebutton

i have the following code in my xaml :
<ToggleButton x:Name="play" Command="{Binding OnPlayButton}" CommandParameter="{Binding ElementName=media , Mode=TwoWay}" HorizontalAlignment="Left" Margin="573,638,0,0" VerticalAlignment="Top" Height="50" Width="50">
<Image Name="MyImage" Source="Images/Control/Play.png"/>
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Checked">
<core:ChangePropertyAction PropertyName="MyImage" Value="{Binding Source=Images/Control/Pause.png}" />
</core:EventTriggerBehavior>
<core:EventTriggerBehavior EventName="Unchecked">
<core:ChangePropertyAction PropertyName="MyImage" Value="{Binding Source=Images/Control/Play.png}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</ToggleButton>
What i'm trying to achieve is change the background of the togglebutton to pause.png when its clicked and change it to play.png when clicked again. I'm getting exception in the xaml , is this the correct way ?
You are almost there with Behaviors. But PropertyName is Not Image It is Source. And TargetObject is MyImage
Your code should be something like below.
<ToggleButton HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="True">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Checked">
<Core:ChangePropertyAction TargetObject="{Binding ElementName=MyImage}" PropertyName="Source" Value="Images/Control/Pause.png" />
</Core:EventTriggerBehavior>
<Core:EventTriggerBehavior EventName="Unchecked">
<Core:ChangePropertyAction TargetObject="{Binding ElementName=MyImage}" PropertyName="Source" Value="Images/Control/Play.png"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
<Image x:Name="MyImage" Source="Images/Control/Play.png" Width="100" Height="100"/>
</ToggleButton>
Good Luck.
You can't use PropertyName="MyImage" as "MyImage" is the name of a control, not a property of ToggleButton!
The easiest way to do what you want is to add the two images one after the other and change visibility according to the control state:
<ToggleButton x:Name="play">
<Image Source="Images/Control/Play.png" Visibility="{Binding IsChecked, ElementName=play, Converter={StaticResource BooleanToVisibilityConverter}" />
<Image Source="Images/Control/Stop.png" Visibility="{Binding IsChecked, ElementName=play, Converter={StaticResource InverterBooleanToVisibilityConverter}" />
</ToggleButton>
On the example above I'm using two converter instances for something that converts a bool value to a Visibility value. You can write your own or just use a 3rd party one like the BooleanToVisibilityConverter from Cimbalino Toolkit.

How to reuse behaviors?

Have two behaviors that change a path's opacity based on its container button's IsEnabled property. I have several buttons that I want to reuse these two behaviors for that have the same path up to their containers. How do I do that?
<Button x:Name="buttonConcentration">
<Canvas Width="42.6667" Height="42.6667">
<Path Opacity="0.2" Width="42.835" Height="42.696" Stretch="Fill"
Data="..." UseLayoutRounding="False">
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior
Binding="{Binding IsEnabled, ElementName=buttonConcentration}"
Value="False">
<Core:ChangePropertyAction PropertyName="Opacity" Value="0.2"/>
</Core:DataTriggerBehavior>
<Core:DataTriggerBehavior
Binding="{Binding IsEnabled, ElementName=buttonConcentration}"
Value="True">
<Core:ChangePropertyAction PropertyName="Opacity" Value="1"/>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Path>
</Canvas>
</Button>
Try setting it on your Window.Resources.
Maybe working over this code you'll be able to achieve what you are expecting:
<Window.Resources>
<Button x:Key="myButtonConcentration" x:Shared="False">
<Canvas Width="42.6667" Height="42.6667">
<Path Opacity="0.2" Width="42.835" Height="42.696" Stretch="Fill"
Data="..." UseLayoutRounding="False">
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior
Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}"
Value="False">
<Core:ChangePropertyAction PropertyName="Opacity" Value="0.2"/>
</Core:DataTriggerBehavior>
<Core:DataTriggerBehavior
Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}"
Value="True">
<Core:ChangePropertyAction PropertyName="Opacity" Value="1"/>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Path>
</Canvas>
</Button>
</Window.Resources>
<Grid>
<ContentControl Name="MyButton1" Content="{StaticResource myButtonConcentration}" />
<ContentControl Name="MyButton2" Content="{StaticResource myButtonConcentration}" />
</Grid>

How to animate a Popup when it hides?

i created a Popup Style for using in my windows 8.1 application. And i applied PopupThemeTransition to it's ChildTransitions Property.
<Style x:Key="AnimatedPopupStyle" TargetType="Popup">
<Setter Property="IsLightDismissEnabled" Value="False"/>
<Setter Property="ChildTransitions">
<Setter.Value>
<TransitionCollection>
<PopupThemeTransition/>
</TransitionCollection>
</Setter.Value>
</Setter>
</Style>
My problem is that it animates when it Opens & not animating when closing. What to do with that for animating the content on hiding ? Do i want to create a Custom Popup control?
NB: I know that PopInThemeAnimation & PopOutThemeAnimation is there . But don't know how to use it on this condition ?
This is not native to the Popup because they typically do not have an exit animation. Having said that, there's no reason you can't add an exit animation to a Popup control.
Try this:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.Resources>
<Storyboard x:Name="ShowPopup">
<PopInThemeAnimation Storyboard.TargetName="MyPopup" />
</Storyboard>
<Storyboard x:Name="HidePopup">
<PopOutThemeAnimation Storyboard.TargetName="MyPopup" />
</Storyboard>
</Grid.Resources>
<Popup x:Name="MyPopup" IsOpen="True"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Popup.Transitions>
<TransitionCollection>
<PopupThemeTransition />
</TransitionCollection>
</Popup.Transitions>
<Grid Height="200" Width="200" Background="Red">
<StackPanel>
<Button Content="Hide (Native)" HorizontalAlignment="Center">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Click">
<Core:ChangePropertyAction
PropertyName="IsOpen"
TargetObject="{Binding ElementName=MyPopup}" />
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Button>
<Button Content="Hide (Storyboard)" HorizontalAlignment="Center">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Click">
<Media:ControlStoryboardAction
Storyboard="{StaticResource HidePopup}"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Button>
</StackPanel>
</Grid>
</Popup>
<StackPanel>
<Button Content="Show Popup (Native)" HorizontalAlignment="Left" VerticalAlignment="Top">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Click">
<Core:ChangePropertyAction
TargetObject="{Binding ElementName=MyPopup}"
PropertyName="IsOpen" Value="True"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Button>
<Button Content="Show Popup (Storyboard)" HorizontalAlignment="Left" VerticalAlignment="Top">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Click">
<Core:ChangePropertyAction
TargetObject="{Binding ElementName=MyPopup}"
PropertyName="IsOpen" Value="True"/>
<Media:ControlStoryboardAction
Storyboard="{StaticResource ShowPopup}"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Button>
</StackPanel>
</Grid>
Using these:
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
xmlns:Media="using:Microsoft.Xaml.Interactions.Media"
Best of luck!

How to make FlipView scrolling only one direction

A simple xaml like this
<FlipView>
<FlipView>
<FlipView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</FlipView.ItemsPanel>
<Rectangle Fill="CornflowerBlue" />
<Rectangle Fill="CornflowerBlue" />
</FlipView>
<FlipView>
<FlipView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</FlipView.ItemsPanel>
<Rectangle Fill="CornflowerBlue" />
<Rectangle Fill="CornflowerBlue" />
</FlipView>
<ListBox>
<Rectangle Fill="CornflowerBlue" />
<Rectangle Fill="CornflowerBlue" />
</ListBox>
</FlipView>
If the main FlipView's current item is a flipview,you can drag and scroll its item on two directions.But if it is a listbox,only one direction is available.means before your finger up,you cannot drag and scroll it on the other direction(Horizental or Vertical).
so how to make flipview's behavier as the same as listbox?
If you extract the default template from the FlipView you can see how the style has these setters:
<Setter
Property="ScrollViewer.IsHorizontalRailEnabled"
Value="False" />
<Setter
Property="ScrollViewer.IsVerticalRailEnabled"
Value="False" />
<Setter
From that you can deduce that setting ScrollViewer.IsHorizontalRailEnabled and ScrollViewer.IsVerticalRailEnabled on your FlipView to True gives you want you ask for.