Отображение изображения по умолчанию в справке по пользовательскому элементу управления? Учебник MSDN

Я делаю учебник в MSDN о том, как создать пользовательский элемент управления.

http://msdn.microsoft.com/en-us/library/cc295235(v=expression.30).aspx

У меня возникли проблемы с отображением изображения по умолчанию на моей кнопке.

Вот код:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace ControlTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();


    }

}
[Description("Represents a custom button control that responds to a Click event. Displays an image using a custom Source property if the Source property is bound to an Image in the template.")]
public class ImageButton : Button 
{ 
    [Description("The image displayed in the button if there is an Image control in the template whose Source property is template-bound to the ImageButton Source property."), Category("Common Properties")] 
    public ImageSource Source 
    { 
        get { return base.GetValue(SourceProperty) as ImageSource; } 
        set { base.SetValue(SourceProperty, value); } 
    } 
    public static readonly DependencyProperty SourceProperty = 
        DependencyProperty.Register("Source", typeof(ImageSource), typeof(ImageButton)); 

    // Constructor:  
    public ImageButton()  
    {  
    if (DesignerProperties.GetIsInDesignMode(this))  
    {  
        this.Source = new BitmapImage(new Uri("images/Image.png", UriKind.Relative));  
        }  
    }  


}

}

Спасибо за вашу помощь! знак равно


person Farnsworth    schedule 10.02.2012    source источник
comment
У вас есть файл images/Image.png?   -  person paparazzo    schedule 10.02.2012
comment
Да. Я дважды проверил. Как вы думаете, это может быть просто ошибка в Blend 4?   -  person Farnsworth    schedule 10.02.2012
comment
Я бы попробовал catch в конструкторе и убедился, что он получает изображение. Попробуйте @images/Image.png.   -  person paparazzo    schedule 10.02.2012


Ответы (1)


Когда вы объявляете DependencyProperty, вы можете указать для него значение по умолчанию, например:

public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof (ImageSource), typeof (ImageButton), new PropertyMetadata("images/Image.png"));

Последний аргумент DependencyProperty.Register — это PropertyMetadata, который можно использовать для указания значения по умолчанию ImageSource. Надеюсь, это поможет.

person Claudiu Constantin    schedule 11.02.2012