tags:

views:

66

answers:

3

Hi

I want Image button with two state(normal , mouse over). that button must change image with Mouse Over event trigger automatically. this image button must be a user control. Also i want to set image for each state form code in which form i use that user control.

Solution is using a template with "Value Converter" but i don't know how?

A: 

If you need a simple rollover effect, you don't need a template for it.. article below has a solution to it..

http://www.c-sharpcorner.com/Resources/Detail.aspx?ResourceId=706 In this article user uses SolidColorBrush, you can use ImageBrush to set image as background of button.

Shoaib Shaikh
coolbuti say again i need a user control
Rev
+2  A: 

Why must this image button be a user control? If a regular button with a new control template is fine, this should work:

<Button>
  <Button.Template>
    <ControlTemplate TargetType="{x:Type Button}">
      <Grid>
        <Image Name="HoverImage" Source="hover_image.png" Visibility="Hidden" />
        <Image Name="DefaultImage" Source="default_image.png" />
      </Grid>
      <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter TargetName="DefaultImage" Property="Visibility" Value="Hidden" />
          <Setter TargetName="HoverImage" Property="Visibility" Value="Visible" />
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Button.Template>
</Button>
Oskar
this solution isn't good for me.if i want use your solution i need make 30 image in my xaml.(15 button)
Rev
Then explain in more detail how you want to use this user control. Its not that obvious.
Oskar
You could put the new control template in a style and use it for your 15 buttons
Wallstreet Programmer
i want make a software phone with wpf.i want use that user corntrol for numeric buttons and some other buttons(config button,dial button)
Rev
A: 

I found This on Code-project-Article(Cool example)

http://www.codeproject.com/KB/WPF/WPF_xaml_taskbar_window.aspx

First He create Wpf-Custom-control(you can create class inherit from Button like this)

 public class ImageButton : Button
{
    private string cornerRadius;
    public string CornerRadius
    {
        get { return cornerRadius; }
        set { cornerRadius = value; }
    }

    private string highlightBackground;
    public string HighlightBackground
    {
        get { return highlightBackground; }
        set { highlightBackground = value; }
    }

    private string pressedBackground;
    public string PressedBackground
    {
        get { return pressedBackground; }
        set { pressedBackground = value; }
    }
}

As second step you must Create template in resource-dictionary(here is code)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Phone.Controls">

<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type local:ImageButton}">
    <ControlTemplate.Resources>
        <Storyboard x:Key="MouseOverButton">
            <ThicknessAnimation Storyboard.TargetName="ButtonBackgroundBorder" 
                                Storyboard.TargetProperty="(Control.Margin)"
                                Duration="0:0:0.05" 
                                FillBehavior="Stop"
                                From="0,0,0,0" To="2,2,2,2" 
                                AutoReverse="True" />
        </Storyboard>
    </ControlTemplate.Resources>
    <Grid x:Name="ButtonOuterGrid">
        <Border x:Name="ButtonBackgroundBorder" 
                CornerRadius="{Binding Path=CornerRadius, RelativeSource={RelativeSource TemplatedParent}}" 
                Background="{Binding Path=HighlightBackground, RelativeSource={RelativeSource TemplatedParent}}" 
                BorderBrush="Black"
                BorderThickness="0.8"
                Opacity="0">
            <Border.BitmapEffect>
                <OuterGlowBitmapEffect GlowColor="#FFFFFFFF" GlowSize="2.75" Noise="0.20"/>
            </Border.BitmapEffect>
        </Border>
        <Border x:Name="ButtonEdgesBorder" CornerRadius="{Binding Path=CornerRadius, RelativeSource={RelativeSource TemplatedParent}}"
                Opacity="1" 
                BorderBrush="Transparent"
                BorderThickness="0" />
        <Border x:Name="ButtonContentBorder" 
                CornerRadius="{Binding Path=CornerRadius, RelativeSource={RelativeSource TemplatedParent}}" 
                Opacity="1" 
                BorderThickness="1">
            <ContentPresenter x:Name="ContentText"
                              Width="Auto" Height="Auto"

                              HorizontalAlignment="Center" 
                              VerticalAlignment="Center"/>
        </Border>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Trigger.Setters>
                <Setter Property="Opacity" TargetName="ButtonBackgroundBorder" Value="1"/>
                <Setter Property="TextElement.Foreground" TargetName="ContentText" Value="Black"/>
            </Trigger.Setters>
        </Trigger>
        <EventTrigger RoutedEvent="Grid.MouseEnter" 
                      SourceName="ButtonOuterGrid">
            <BeginStoryboard Storyboard="{StaticResource MouseOverButton}"/>
        </EventTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="ImageButton" TargetType="{x:Type Button}">
    <Setter Property="Template" Value="{StaticResource ButtonTemplate}" />
</Style>

And this is last Step, in xaml file you must insert this custom-control

<ImageButton x:Name="btnConfigs"
                      Style="{StaticResource ImageButton}"
                      Width="25" Height="25"
                      VerticalAlignment="Top"
                      HorizontalAlignment="Right"
                      Margin="0,31.125,16.418,0">
            <Image x:Name="ImgConfigs"
                   Stretch="Fill"/>
        </ImageButton >

and in cs file do this

 this.ImgConfigs.Source="any imag-source" 

also we can change this image-source on btnconfig-click event

With special thanks from Murray-Foxcroft for create that article

Rev