WP7 - Image Click Event? - silverlight

Is there a way to assign a click event to images? I would like to assign events to the delete and search buttons inside of my listbox that displays my data. Is there a way to do this using the image control or do I have to create a style in BLEND for a button?
<ListBox x:Name="lbPills" ItemsSource="{Binding pillItemsCollection}" SelectionChanged="lbPills_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
<TextBlock FontFamily="Segoe WP Semibold" FontWeight="Bold" FontSize="30" VerticalAlignment="Top" Margin="20,0">*</TextBlock>
<StackPanel>
<TextBlock x:Name="ItemText" Text="{Binding Name}" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</StackPanel>
<Image Source="Images/delete.png" Margin="10,0"/>
<Image Source="Images/search.png" Margin="10,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

To my knowledge there are no listeners on the Image itself for click and gesture events (they will have to be attached via Gestures as previously mentioned). One way to approach this is to re-template the button:
<Button>
<Button.Template>
<ControlTemplate>
<Image Source="Images/delete.png" Margin="10,0"/>
</ControlTemplate>
</Button.Template>
</Button>
In setting the template on the button you will override the default template used by the phone (which has the extra padding, thick border, etc.). Using this method will allow you to tie into the button click event.

You can use the gesture listener to detect tap (click) events. A walkthrough here.
WP7 Tip of the Day: Silverlight Toolkit: Gestures
Alternatively, you can place your image into a Button control and retemplate it in blend to have the appearance you want.

Handle the ManipulationCompleted event (which is any tap, double-tap, swipe, caress or fondle) to your image(s). So:
<Image Source="Images/delete.png" Margin="10,0"/> becomes <Image x:Name="ImageDelete" ManipulationCompleted="ImageDelete_ManipulationCompleted" Source="Images/delete.png" Margin="10,0"/>. Then in the ImageDelete_ManipulationCompleted handler, track from whence it came in from the sender and do your thing.
If you want to only track a swipe instead of a tap, just do an if statement on the e.IsInertial from ManipulationCompletedEventArgs.

If your ListBox is not in a Panorama control, then you could just handle the SelectionChanged event for the list box and then display the Delete and Search options in an ApplicationBar for the page.

I do it with MouseLeftButtonDown and MouseLeftButtonUp. It replaces the tab or click on the device like you have click event on pc with mouse. It works in my app. Just try this and you will be happy, I guess.

It worked this way for me (Making Padding="-10" removed the button border and padding inside the button)
<Button x:Name="Channells" Click="Thumb_Click" Padding="-10" >
<Image
VerticalAlignment="Center"
Source="Assets/Images/thumb2.jpg"/>
</Button>
*.cs
void Thumb_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Thumb Clicked");
}

Related

WP8 LongListSelector firing SelectionChanged event when CheckBox is clicked

I have a simple data template for the new Windows Phone 8 LongListSelector as follows:
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,-6,0,-10">
<CheckBox x:Name="ToDoCheckBox" Margin="0" IsChecked="{Binding ItemIsComplete}" Checked="ToDoCheckBox_Checked"/>
<TextBlock Text="{Binding ItemName}" TextWrapping="NoWrap" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeExtraLarge}"/>
</StackPanel>
</DataTemplate>
There are suppsed to be a few more items in the template, that's why CheckBox's content property hasn't been used.
Now, I have page navigation implemented on SelectionChanged event. Problem is that, SelectionChanged even is fired even when the CheckBox is clicked. Earlier questions seem to discuss exactly opposite issue. I do not want SelectionChanged to be fired. Or, at least I don't want page to navigate on CheckBox events. I just want to have checkbox checked or unchecked event. How do I achieve that?
Selection is a bad way to trigger navigation. Without digging into that, you can avoid this by triggering the navigation when the other item in the template is tapped.
e.g.
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,-6,0,-10">
<CheckBox x:Name="ToDoCheckBox" Margin="0" IsChecked="{Binding ItemIsComplete}" Checked="ToDoCheckBox_Checked"/>
<TextBlock Text="{Binding ItemName}"
TextWrapping="NoWrap"
Style="{StaticResource PhoneTextExtraLargeStyle}"
FontSize="{StaticResource PhoneFontSizeExtraLarge}"
Tap="TriggerNavigationToThisItem"/>
</StackPanel>
</DataTemplate>
The above assumes you have an event called TriggerNavigationToThisItem which will trigger the navigation. You could also add a command on the Item and bind to that to trigger the navigation.
If you have a more complex template you could encapsulate them in a container (like a Grid) and then have that trigger the navigation.
You can check the original source of en every selection changed event. If it is the checkbox - simply skip execution.
Also. Don't use native "LongListSelector.Selected" property and selection event at all. It has completely no scalability (for example for multiselection). Implement it with your own Tap event handling.

WPF: How to make an overlay control with transparent canvas and clickable child

I want to make a custom control which will be used as an overlay. The control should contain a couple of child controls which should be drawn and should be clickable as usual. But everything else in the control should be transparent and "clickable-through".
Here is how I try to achieve this... First, I'm using PreviewMouseDown\Up\Move events in the window where the overlay is going to be placed. I want these events to "go through" transparent part of my custom control, but stop at not-transparent (for example at my button). Second, here is the xaml for my control (root UserControl node was left untouched):
<Canvas Background="transparent" IsHitTestVisible="true">
<Button Canvas.Left="384" Canvas.Top="34" Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" IsHitTestVisible="True" />
<TextBlock Canvas.Left="27" Canvas.Top="105" Height="36" Name="textBlock1" Text="TextBlock" Width="432" FontSize="24" IsHitTestVisible="False" Foreground="Red" FontWeight="Bold" />
</Canvas>
However if I set Canvas' IsHitTestVisible to false, the whole control including button becomes "unhittable". If set the it to true my all the tunneling events stop at custom control and button becomes unclickable.
What is the right way to achieve this kind of behavior?
Is it possible to do so without subclassing canvas (or any other panel)?
You should set the background of the Canvas to null (or just no background, null is default). Transparent is "visible" to the mouse clicks.

WPF: Visual studio like error buttons

I want to get this.
buttons http://www.shrani.si/f/X/6Y/24Jhn9D3/buttns.png
Everything works so far, buttons act as filter and are bind to the grid control.
All i want is the icons and counter on the button.
Whats the correct way of implementing those?
<ToggleButton x:Name="IsErrorShown" Margin="4" Width="100" Content="{lex:LocText Errors, Assembly=Client}">
I have tried adding image like this:
<ToggleButton x:Name="IsErrorShown" Margin="4" Width="100" Content="{lex:LocText Errors, Assembly=Client}">
<StackPanel>
<Image Source="Resources/Warning"/>
</StackPanel>
</ToggleButton>
but i get error that prop. Content is defined more then once.
A WPF Button (or ToggleButton) is a content control, into which you can put anything.
I haven't checked, but these buttons probably have a horizontal stack panel or a DockPanel, with an Image and then one or two TextBlocks. You could make a template for these, and also use binding to set the TextBlock Text content from your viewmodel.
Snoop ( http://snoopwpf.codeplex.com/ ) is a great tool for finding out how other people have built things in WPF.
The Adam Nathan WPF book is excellent, and if you don't have it you should get it. http://www.amazon.co.uk/Windows-Presentation-Foundation-Unleashed-WPF/dp/0672328917
Here's an example:
<ToggleButton Height="24" Width="100">
<DockPanel>
<Image Source="c:\\temp\\me.jpg" Margin="3"/>
<TextBlock Text="20 Errors"/>
</DockPanel>
</ToggleButton>

WPF Rectangle does not have a Click event

It seems that the WPF Rectangle shape does not have the Click event defined. What am I supposed to use instead?
It does have MouseUp, but it's not quite the same behavior.
If you're not happy with MouseDown and MouseUp, perhaps you could just put the Rectangle in a Button and handle the Button's Click event?
<Button>
<Button.Template>
<ControlTemplate>
<Rectangle .../>
</ControlTemplate>
</Button.Template>
</Button>
It really depends with the behavior you're after. Please elaborate if needs be.
To add click handing to a Rectangle itself, you can use the InputBindings property:
<Rectangle Fill="Blue" Stroke="Black">
<Rectangle.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding FooCommand}"/>
</Rectangle.InputBindings>
</Rectangle>
This will cause the FooCommand to be executed when the rectangle is clicked. Handy if you're using MVVM!
I was looking for the related DoubleClick event and came across this suggestion to simply embed the object of interest in a ContentControl.
Here is their example (with a border, which also did not support click/double click).
<ContentControl MouseDoubleClick="OnDoubleClick">
<Border Margin="10" BorderBrush="Black" BorderThickness="2">
<Grid Margin="4">
<Rectangle Fill="Red" />
<TextBlock Text="Hello" FontSize="15" />
</Grid>
</Border>
</ContentControl>
Rectangle has event Tapped, which works fine. In designing universal apps for Windows 8.1, there are new events. Tapped, DoubleTaped, RightTapped and Holding.

How can I avoid the bluish border when clicking a HyperlinkButton in Silverlight?

I have a Silverlight menu for my application with an image as the background. I use some empty HyperlinkButton at a specific position and size to simulate a real button on the image (think as a HTML image-map):
<HyperlinkButton x:Name="Portfolio" Width="86" Height="40" Canvas.Top="50" NavigateUri="/portfolio"/>
<HyperlinkButton x:Name="Analysis" Width="79" Height="40" Canvas.Top="50" Canvas.Left="124" NavigateUri="/analysis" BorderThickness="0"/>
<HyperlinkButton x:Name="News" Width="77" Height="40" Canvas.Top="50" Canvas.Left="240" NavigateUri="/news"/>
<HyperlinkButton x:Name="Questions" Width="80" Height="40" Canvas.Top="50" Canvas.Left="357" NavigateUri="/questions"/>
<HyperlinkButton x:Name="Companies" Width="80" Height="40" Canvas.Top="50" Canvas.Left="477" NavigateUri="/companies"/>
The problem is when I click these buttons it shows a bluish border corresponding to the hyperlink button area during the click event. There is a way I can avoid showing that?
I found the answer in other blog, just set IsTabStop="False" in the HyperLinkButton instance.
For info on styling controls, see http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-7-using-control-templates-to-customize-a-control-s-look-and-feel.aspx (skip down to the Customizing Controls using Control Templates section halfway down). If you want to start with the default style (usually a good idea--start here and add/change/remove things until you get what you want) look here: http://msdn.microsoft.com/en-us/library/cc296242(VS.95).aspx.
In this case, I believe the offender is the "FocusVisualElement." You could either change the color of it, set visibility to "Collapsed," or remove/change the "Focused" state so the storyboard isn't run.
You can edit a template of HyperlinkButton in Blend:
1.right click on control choose "Edit Template->Edit a copy"
2.in "States" panel click "Pressed"
3.change property "Stroke" of rectangle named "FocusVisualElement" from solid color to "No brush"
http://silverlight.net/forums/t/40896.aspx

Resources