0

I was trying to make re-usable tamplete for image button, and found this great advice, but I wasn't able to make it work. Can you please advice my, what am I doing wrong? I am pretty newbie with WPF, so it may be something really basic, but I can't see it.

My ImageButton.cs class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ImageBut
{
    public class ImageButton : Button
    {
        static ImageButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton),
                new FrameworkPropertyMetadata(typeof(ImageButton)));
        }


        public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        public static readonly DependencyProperty ImageProperty =
            DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(default(ImageSource)));

    }
}

My generic.xaml in Themes subfolder.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:my="clr-namespace:ImageBut;assembly=ImageBut"
                    >

    <Style x:Key="{x:Type my:ImageButton}" TargetType="{x:Type my:ImageButton}">
        <Setter Property="Width" Value="32"></Setter>
        <Setter Property="Height" Value="32"></Setter>
        <Setter Property="BorderThickness" Value="0"></Setter>
        <Setter Property="Margin" Value="4,0,0,0"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Image Source="{TemplateBinding Image}" Stretch="Fill"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <!--  Some triggers ( IsFocused, IsMouseOver, etc.) -->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>    
</ResourceDictionary>

My MainWindow.xaml

<Window x:Class="ImageBut.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:ImageBut;assembly=ImageBut"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <my:ImageButton Image="C:\Users\Martin\Source\Workspaces\Digiterm\ImageBut\ImageBut\1_1.bmp"></my:ImageButton>

    </Grid>

</Window>

Here are my Project properties and here is snapchat of Solution Explorer.

Exception I am getting:

1>C:\Users\Martin\Source\Workspaces\Digiterm\ImageBut\ImageBut\MainWindow.xaml(7,10): error MC3074: The tag 'ImageButton' does not exist in XML namespace 'clr-namespace:ImageBut;assembly=ImageBut'. 

Thank you!

Community
  • 1
  • 1
MartinVotruba
  • 343
  • 2
  • 17

1 Answers1

0

You shouldn't need the assembly name in the namespace declaration.

Instead of this:

xmlns:my="clr-namespace:ImageBut;assembly=ImageBut"

try this:

xmlns:my="clr-namespace:ImageBut"

As an aside, the standard WPF Button control supports any kind of content, including images. So you can have an Image control inside a Button, like so:

<Button>
    <Image Source="C:\Users\Martin\Source\Workspaces\Digiterm\ImageBut\ImageBut\1_1.bmp" />
</Button>

EDIT

In your generic.xaml file you have set the TargetType of the ControlTemplate to the wrong thing, which is causing a compile-time error. Instead of:

<ControlTemplate TargetType="{x:Type Button}">

it should be:

<ControlTemplate TargetType="{x:Type my:ImageButton}">

After fixing this, the project compiled successfully on my computer.

Steven Rands
  • 5,160
  • 3
  • 27
  • 56
  • I have tried the suggested change of my and unfortunerelly nothing happened, I still get the same Error. What I want to achieve is to make specific control, so I wont have to define the same triggers and style but with different images for every button. I will have to modify the class and xaml but firstly I want to make it work at least like this. – MartinVotruba Jan 19 '15 at 13:34
  • Not sure why that didn't work. Is your **ImageButton** control in the same assembly as your **Window**? – Steven Rands Jan 19 '15 at 13:40
  • I don't know how to find out. It should be... If you won't mind, you can examine my project - http://goo.gl/J3zmTB Thanks! – MartinVotruba Jan 19 '15 at 13:55