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
Related
I am trying to place a ContextMenu within the DataTemplate of my ListBox. The ListBox is placed within a PivotItem. For some reason, the ContextMenu does not show up with an item in the ListBox is pressed. I am unsure of the error since nothing shows in the Error List or while debugging.
MainPage.xaml
<phone:PhoneApplicationPage.Resources>
<Style x:Key="MyStyle" TargetType="ListBoxItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property ="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="LayoutRoot" Background="{TemplateBinding Background}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Opacity" Duration="0" To=".5" />
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="brd"
Storyboard.TargetProperty="BorderThickness">
<DiscreteObjectKeyFrame KeyTime="0" Value="2" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="brd" CornerRadius="10" BorderBrush="{StaticResource PhoneAccentBrush}" Width="Auto" BorderThickness="{TemplateBinding BorderThickness}">
<Image x:Name="recentImage" Source="{Binding Source}" Margin="12" Width="115"/>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
<phone:PivotItem Header="recent">
<ListBox x:Name="Recent" ItemsSource="{Binding Pictures}" Margin="8"
SelectionChanged="recent_SelectionChanged" toolkit:TiltEffect.IsTiltEnabled="True"
ItemContainerStyle="{StaticResource MyStyle}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu x:Name="imgListContextMenu">
<toolkit:MenuItem Header="share" Tap="shareContextMenuItem_Tap"/>
<toolkit:MenuItem Header="favorite" Tap="favoriteContextMenuItem_Tap"/>
<toolkit:MenuItem Header="set as start screen" Tap="setAsStartScreenContextMenuItem_Tap"/>
<toolkit:MenuItem Header="delete" Tap="deleteContextMenuItem_Tap"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</phone:PivotItem>
Also, is Tap the best event handler method to use within each of the ContextMenu item?
why not use
<ListBox.ContextMenu>
<ContextMenu>...
</ListBox.ContextMenu>
Since my ListBox has its own custom style, I placed the contextmenu under where the image binding occurs.
Under the closing tag of VisualStateManager..
</VisualStateManager.VisualStateGroups>
<Border x:Name="brd" CornerRadius="10" BorderBrush="{StaticResource PhoneAccentBrush}" Width="Auto" BorderThickness="{TemplateBinding BorderThickness}">
<Image x:Name="recentImage" Source="{Binding Source}" Margin="12" Width="115"/>
</Border>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu x:Name="imgListContextMenu">
...
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
Please tell me any one How can i change the scroll bar color in windows phone 7 and also i want to customize the scroll bar in windows phone 7.
Have you tried using the "background property in your xaml code where the scroll bar lies?
try this:
<ScrollBar Background="Green" HorizontalAlignment="Left" Margin="299,144,0,0" VerticalAlignment="Top"/>
don't use this whole thing what i want you to take from it is you should have a line of xaml code similar to this just enter the background property as i did. hope this is more clear.
If you are happy with coding it into XAML, find your answer here. Or else the shortest and the easiest way is to use Expression Blend.
Also a snippet I found elsewhere:
<ListBox x:Name="myListBox">
<ControlTemplate TargetType="ScrollViewer">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ScrollStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:00.5"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Scrolling">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="VerticalScrollBar"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="HorizontalScrollBar"/>
</Storyboard>
</VisualState>
<VisualState x:Name="NotScrolling"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Margin="{TemplateBinding Padding}">
<ScrollContentPresenter x:Name="ScrollContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}"/>
<ScrollBar x:Name="VerticalScrollBar" Background="Blue" HorizontalAlignment="Right" Height="Auto" IsHitTestVisible="False" IsTabStop="False" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Opacity="0" Orientation="Vertical" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{TemplateBinding VerticalOffset}" ViewportSize="{TemplateBinding ViewportHeight}" VerticalAlignment="Stretch" Width="5"/>
<ScrollBar x:Name="HorizontalScrollBar" Background="Blue" HorizontalAlignment="Stretch" Height="5" IsHitTestVisible="False" IsTabStop="False" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Opacity="0" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{TemplateBinding HorizontalOffset}" ViewportSize="{TemplateBinding ViewportWidth}" VerticalAlignment="Bottom" Width="Auto"/>
</Grid>
</Border>
</ControlTemplate>
Hope all this helps you :)
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;
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.
I made a simple button template from the rectangle with "Make into control../Button" command. Now, I need several buttons from that template but each button must have different background image. I tried to do that in Blend4 but when I change the button background, button holds to a template background (or none, whichever is set in template), ignoring the image I have set for that specific button.
Button Template:
btnMenu (No brush)
-rectangle (No brush)
-[ContentPresenter] (some text)
I apreciate any advice.
Style:
<Style x:Key="btnStyleMenuHome" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="btnMenu" Width="90" Height="70" Margin="0" VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.Background>
<ImageBrush Stretch="None"/>
</Grid.Background>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(TileBrush.Stretch)" Storyboard.TargetName="rectangle">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Stretch>Uniform</Stretch>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="rectangle" RadiusY="5" RadiusX="5" StrokeThickness="0" Width="90" Height="70" VerticalAlignment="Center" HorizontalAlignment="Center">
<Rectangle.Fill>
<ImageBrush Stretch="None" ImageSource="images/someImage.png"/>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter VerticalAlignment="Bottom" Margin="7,0" d:LayoutOverrides="Width" Height="25" HorizontalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>
<ImageBrush Stretch="None"/>
</Setter.Value>
</Setter>
</Style>
Have a look at Custom UserControl Property used by child element