How to add the click event on textblock of listbox? - windows-phone-7

I am developing window phone 7 application. I have a listbox in my application. I am doing dynamic binding with that listbox. The code of listbox is as follows:
<ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding ID}" Width="440"></TextBlock>
<TextBlock Text="{Binding IsCompany}" Width="440"></TextBlock>
<TextBlock Foreground="Red" Text="{Binding CompanyORIndividual}" Width="440"></TextBlock>
<TextBlock Text="{Binding MicrosoftContact}" Width="440"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Template>
<ControlTemplate>
<ScrollViewer HorizontalScrollBarVisibility="Visible">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>
</ListBox>
This listbox is inside the pivot control. I want to add the link or click event one the following textblock.
<TextBlock Foreground="Red" Text="{Binding CompanyORIndividual}" Width="440"></TextBlock>
So that after clicking on the CompanyORIndividual textblock the user should be navigated to the other xaml page. How to do this. Can you please provide me the code with the event handler ? Can you please provide me nay code or link through which I can resolve the above issue ? If I am doing anything wrong then please guide me.If there is any better idea than my above question then please suggest it.

you can wrap the TextBlock with a Grid, and give it a Transparent color. Then you attach a NavigateToPageAction to the Grid, something like this,
xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
<Grid Background="Transparent">
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="MouseLeftButtonDown">
<ic:NavigateToPageAction TargetPage="/Views/YourView.xaml" />
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
<TextBlock Foreground="Red" Text="{Binding CompanyORIndividual}" HorizontalAlignment="Left"/>
</Grid>
The reason that you need to give the Grid a color is because doing so will be able to receive mouse click. Please give it a try and let me know if you have problems using it.
Alternatively, you can wrap the TextBlock with a Button, then handle the Button's click event.
Update:
Matt is right about the thing that using MouseLeftButtonDown/MouseLeftButtonUp may sometimes be bad.
In my own project I have created a simple style for a button which acts like a clickable TextBlock. Also another good thing of using a button is it can receive the TiltEffect and allow Commanding.
<Style x:Key="TextButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}"/>
<Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
<Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiLight}"/>
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeLarge}"/>
<Setter Property="Padding" Value="{StaticResource PhoneTouchTargetOverhang}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>

I'd recommend using a Tap gesture (from the Silverlight Toolkit) on the TextBlock. This will avoid issues with incorrectly detecting mousebutton up or down events when the user is scrolling the listbox. In my opinion this also provides a better user experience than detecting SelectedItem changed on the Listbox.

Here is what I did using VS2010.
Select the TextBlock control in the xaml window.
In the properties window, select Events.
Locate MouseLeftButtonUp, and double-click in the empty value text box, that will lead you to the xaml.cs code behind window, and will create a new method.
Place this.Close(); in that method.

As workaround for a similar problem I faced in WP8 (Silverlight), I used a Button with transparent background and a Click event handler.
Now the problem with this is that, whenever the user touches/clicks on the button, a solid PhoneAccent color is set as the button's background (for the time period user holds it).
To avoid this, you can set the button's ClickMode="Pressed" and in Click event set the background to transparent with MyButton.Background = new SolidColorBrush(Colors.Transparent);
Background is set to transparent whenever the Click event is fired with Button in Pressed state so the background color is set to Button when it is in that state. Not setting the ClickMode="Pressed" will not set Background color when Button is in pressed state.

Related

Clickable image in Windows 8.1 RT XAML

I'd like to make an image button. With a image for the default status and a different image for the pressed status. With no other pressed highlights.
How can I implement that?
You can achieve that by defining your own style for Button control. I'll show you my example.
First, I have added two images to my project.
Normal.png
Pressed.png
Then, I have definied new style in App.xaml file based on default Button style.
<Application.Resources>
<ImageBrush x:Name="ImageButtonNormal" ImageSource="Assets/ImageButton/Normal.png" />
<ImageBrush x:Name="ImageButtonPressed" ImageSource="Assets/ImageButton/Pressed.png" />
<Style x:Key="ImageButtonStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="Grid" Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ImageButtonPressed}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Border"
Background="{StaticResource ImageButtonNormal}" >
<ContentPresenter x:Name="ContentPresenter"
AutomationProperties.AccessibilityView="Raw"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
As you can see, I have definied 2 ImageBrush objects for 2 button states. The look of the button is defined as Border with some content. We want to change the background of the Border. VisualStateGroups will handle this. I have added Storyborad in VisualState named Pressed to change the background of the Border just by switching ImageBrush. Simple!
The last thing you have to do is to apply the new style to your Button in XAML. I did it in MainPage.xaml.
<Button Style="{StaticResource ImageButtonStyle}" />
You can user "PointerPressed" and "PointerReleased" events of the Image control to do that and change the Source property in each one. This will make Image flickers, so you can solve this problem by checking this Link

Focus changed on LoadMore Button click in Windows Phone Application Page

In my Windows Phone application I have a ListBox with Load More button, in one of my pages. At the bottom of the page a TextBox and a button is present. when I click on the Load More button at the bottom of listbox, the focus automatically goes to the textbox and the keypad is shown. I don't need to go the focus to the textbox on buttonclick. How this problem occures and how can I overcome this problem.
The xaml of style used for the listbox shown below
<phone:PhoneApplicationPage.Resources>
<Style x:Key="ListBoxMore" TargetType="ListBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Visible"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ScrollViewer x:Name="ScrollViewer">
<StackPanel>
<ItemsPresenter/>
<Border BorderBrush="Black" BorderThickness="0,0.5">
<Button Content="Load More..." Name="btnLoadMore" Visibility="Visible" Background="White" Foreground="#FF176DDC" Click="btnLoadMore_Click"></Button>
</Border>
</StackPanel>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
The xaml of the listbox shown below
<ListBox x:Name="userDetailsList" SelectionMode="Multiple" Style="{StaticResource ListBoxMore}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border BorderThickness="0,0.5,0,0" BorderBrush="Black" Width="440">
<Grid Width="430">
<Image Source="{Binding UserImage}"/>
<TextBlock Text="{Binding UserName}"/>
<TextBlock Text="{Binding Details}"/>
<TextBlock Text="{Binding Date}"/>
</Grid>
</Border>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When I click on loadmore button the focus goes to the textbox that placed below the listbox. There is only one textbox is there.

How to Default Scroll Position in ScrollViewer/ListBox in WP7

I'm building a WP7 application using the Panorama control. I'm fairly new to WP7 so I might be missing something simple. My Panorama control is bound to ViewModel and PanoramaItems are added at runtime when a property of that ViewModel is populated with data via web service request. I'd like to have a section at the top of each PanoramaItem that holds a refresh button etc.
I'm able to add an item to the top of the ListBox using a Style but I would like to have that item scrolled off the top until a user pulls it down. Is there a way to set a default scroll position in the Style or in Template? I've read a few example of scrolling to a particular item once there is data using listBox.ScrollToItem, but that didn't work in my test application, or getting a handle on the ScrollViewer and using ScrollToVerticalOffset.
<phone:PhoneApplicationPage.Resources>
<Style x:Key="StoryStyle" TargetType="ListBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}">
<StackPanel>
<Button Content="Refresh"></Button>
<ItemsPresenter/>
</StackPanel>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<controls:Panorama ItemsSource="{Binding Programs}" x:Name="AllPrograms">
<controls:Panorama.Title>
<TextBlock></TextBlock>
</controls:Panorama.Title>
<controls:Panorama.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" />
</DataTemplate>
</controls:Panorama.HeaderTemplate>
<controls:Panorama.ItemTemplate>
<DataTemplate>
<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Stories}" Style="{StaticResource StoryStyle}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="80">
<TextBlock Text="{Binding SensibleTitle}" TextWrapping="Wrap" />
<TextBlock><Run Text="{Binding Duration, StringFormat='hh\\:mm\\:ss'}"></Run><Run Text=", "/><Run Text="{Binding DisplayDate, StringFormat='MMMM dd, yyyy'}"/></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</controls:Panorama.ItemTemplate>
</controls:Panorama>
</Grid>
Try changing SelectedIndex for ListBox.

How can I make something like this? (Tiles inside the app) Windows phone

I'm sorry if the question title wasnt clear, but I'm trying to make something like this. I don't know if they are tiles or images inside a WrapControl:
I was thinking of making such thing with a wrap panel and each one of those blocks as a stackpanel. but I'm not sure if thats the right approach.
is there a control to do such thing?
You are on the right track. WrapPanel is the way to go :)
To make each block more interesting, you can take a look at the HubTile control from the latest windows phone toolkit. Whatever controls/panels you are using, just remember the size should be 173*173.
Use a ListBox
In one of my projects I created a ListBox that does all this. The reason that I use a ListBox is because ListBox has a SelectedItem propery which tells me which tile is tapped by the user. Also another reason is ListBoxItems can receive the nice tilt effect.
Baiscally you just need to create a tile-like ListBoxItem style and apply it to the ListBox's ItemContainerStyle, also you need to set the ListBox's ItemsPanel to be a WrapPanel.
How it looks
The ListBoxItem Style
<Style x:Key="TileListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="FontSize" Value="64"/>
<Setter Property="Margin" Value="12,12,0,0"/>
<Setter Property="Background" Value="{StaticResource PhoneAccentBrush}"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Width" Value="173"/>
<Setter Property="Height" Value="173"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid>
<Rectangle Fill="{TemplateBinding Background}"/>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The ListBox
<!-- set its ItemContainerStyle which is the style for each ListBoxItem -->
<ListBox ItemContainerStyle="{StaticResource TileListBoxItemStyle}">
<!-- set its ItemsPanel to be a WrapPanel -->
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>
<Grid>
<TextBlock Text="Messages" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Path Data="M1.4901163E-05,9.8579922 L50.000015,46.316994 L100.00002,9.8579922 L100.00002,62.499992 L1.4901163E-05,62.499992 z M0,0 L100,0 L50,36.458 z" Fill="White" Height="38.125" Stretch="Fill" UseLayoutRounding="False" Width="61" d:IsLocked="True" />
<TextBlock Text="12" Margin="4,0,0,8" />
</StackPanel>
</Grid>
</ListBoxItem>
<ListBoxItem/>
<ListBoxItem/>
<ListBoxItem/>
<toolkit:HubTile Title="Me ☺" Message="..." Notification="new messages!" Source="xxx.jpg" Margin="12,12,0,0" />
</ListBox>
You can see the last item is actually a HubTile.
Hope ths helps! :)

Change the background in ListPicker´s FullMode in WP7?

Anyone knows how to easily change the background in ListPicker´s FullMode? Do I need to set a style to be able to do that? I´ve tried that, but can´t find the right place to set the background... If it is a style that needs to be used, where exactly in that style can I set the background? Any help would be highly appreciated :)
Here is the standard Style for the ListPicker:
<Style TargetType="toolkit:ListPicker">
<Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>
<Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Margin" Value="{StaticResource PhoneTouchTargetOverhang}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="toolkit:ListPicker">
<StackPanel>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="PickerStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Expanded">
<Storyboard>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="Border"
Storyboard.TargetProperty="Background"
Duration="0">
<DiscreteObjectKeyFrame
Value="{StaticResource PhoneTextBoxEditBackgroundColor}"
KeyTime="0"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="Border"
Storyboard.TargetProperty="BorderBrush"
Duration="0">
<DiscreteObjectKeyFrame
Value="{StaticResource PhoneTextBoxEditBorderBrush}"
KeyTime="0"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentControl
Content="{TemplateBinding Header}"
ContentTemplate="{TemplateBinding HeaderTemplate}"
Foreground="{StaticResource PhoneSubtleBrush}"
FontSize="{StaticResource PhoneFontSizeNormal}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="0 0 0 8"/>
<Grid>
<Border
x:Name="Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding Background}"
BorderThickness="2">
<Canvas x:Name="ItemsPresenterHost" MinHeight="46">
<ItemsPresenter x:Name="ItemsPresenter">
<ItemsPresenter.RenderTransform>
<TranslateTransform x:Name="ItemsPresenterTranslateTransform"/>
</ItemsPresenter.RenderTransform>
</ItemsPresenter>
</Canvas>
</Border>
<Popup x:Name="FullModePopup">
<Border Background="{StaticResource PhoneChromeBrush}"> <!-- Popup.Child should always be a Border -->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ContentControl
Grid.Row="0"
Content="{TemplateBinding FullModeHeader}"
Foreground="{StaticResource PhoneForegroundBrush}"
FontFamily="{StaticResource PhoneFontFamilySemiBold}"
FontSize="{StaticResource PhoneFontSizeMedium}"
HorizontalAlignment="Left"
Margin="24 12 0 0"/>
<ListBox
x:Name="FullModeSelector"
Grid.Row="1"
ItemTemplate="{TemplateBinding ActualFullModeItemTemplate}"
FontSize="{TemplateBinding FontSize}"
Margin="{StaticResource PhoneMargin}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/> <!-- Ensures all containers will be available during the Loaded event -->
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
</Border>
</Popup>
</Grid>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
There is no way to do this through templating.
The only way to do this is by changing the source of the control. (You'll need to change the Style for TargetType="controls:ListPicker".)
Specifically, you'll ned to change the contents of the FullModePopup.
I was wondering this same thing.
While there is no way of directly changing the background color of the ListPicker, it does follow along with the color of the PhoneChromeBrush resource, and thus you can override the color of all of those in your app to change the color of this background.
For example:
((SolidColorBrush)App.Current.Resources["PhoneChromeBrush"]).Color = Colors.Cyan;

Resources