用户控件上的WPF按钮属性未显示属性窗格

时间:2016-06-14 19:11:36

标签: c# wpf xaml user-controls dependency-properties

我已经连续几天撞墙了,我似乎无法找到适合我问题的任何信息。

所以,我有这个工具栏Usercontrol,它意味着被放入一个应用程序。此工具栏有一个名为“FullExtentButton”的属性,它是对按钮的引用。我想要的是在工具栏用户控件的设计器属性窗格中公开此按钮的属性,以便开发人员可以直接从设计器设置属性。

在WinForms中,这很容易做到。 WPF,不是那么多(除非我只是盲目)。

在我的工具条形码中:

[Category("Standard Buttons")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public MyToolbarButton FullExtentButton
{
    get;
    private set;
}

此值在用户控件的构造函数中设置:

public MyToolbar()
{
    InitializeComponent();

    FullExtentButton = new MyToolbarButton("FullExtent", "/Utilities;component/Resources/full_extent_16x16.png");
}

按钮本身非常简单:

public class MyToolbarButton
    : Freezable
{
    #region Dependency Properties.
    /// <summary>
    /// Dependency property for the <see cref="IsVisible"/> property.
    /// </summary>
    public static DependencyProperty IsVisibleProperty =     DependencyProperty.Register("IsVisible", typeof(bool), typeof(MyToolbarButton),
                                                                                                 new FrameworkPropertyMetadata(true,
                                                                                                                           FrameworkPropertyMetadataOptions
                                                                                                                           .BindsTwoWayByDefault, Visible_Changed));
/// <summary>
/// Dependency property for the <see cref="IsEnabled"/> property.
/// </summary>
public static DependencyProperty IsEnabledProperty = DependencyProperty.Register("IsEnabled", typeof(bool), typeof(MyToolbarButton),
                                                                                 new FrameworkPropertyMetadata(true,
                                                                                                               FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, Enabled_Changed));

/// <summary>
/// Dependency property for the <see cref="ToolTip"/> property.
/// </summary>
public static DependencyProperty ToolTipProperty = DependencyProperty.Register("ToolTip", typeof(string), typeof(MyToolbarButton),
                                                                                 new FrameworkPropertyMetadata(string.Empty,
                                                                                                               FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, ToolTip_Changed));

/// <summary>
/// Dependency property for the <see cref="Glyph"/> property.
/// </summary>
public static DependencyProperty GlyphProperty = DependencyProperty.Register("Glyph", typeof(ImageSource), typeof(MyToolbarButton),
                                                                                 new FrameworkPropertyMetadata(null,
                                                                                                               FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, Glyph_Changed));

/// <summary>
/// Dependency property for the <see cref="ID"/> property.
/// </summary>
public static DependencyProperty IDProperty = DependencyProperty.Register("ID", typeof(string), typeof(MyToolbarButton),
                                                                          new FrameworkPropertyMetadata(string.Empty,
                                                                                                        FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

/// <summary>
/// Dependency property for the <see cref="ClickedCommand"/> property.
/// </summary>
public static DependencyProperty ClickedCommandProperty = DependencyProperty.Register("ClickedCommand", typeof(IMyRelayCommand<string>), 
    typeof(MyToolbarButton),
    new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
#endregion

#region Variables.
// The default image source for the button glyph.
private readonly Uri _defaultImageSource;
#endregion

#region Properties.
/// <summary>
/// Property to set or return the command to execute when the button is clicked.
/// </summary>
public IMyRelayCommand<string> ClickedCommand
{
    get
    {
        return (IMyRelayCommand<string>)GetValue(ClickedCommandProperty);
    }
    set
    {
        SetValue(ClickedCommandProperty, value);
    }
}


/// <summary>
/// Property to set or return the ID of the button.
/// </summary>
public string ID
{
    get
    {
        object value = GetValue(IDProperty);

        return value == null ? string.Empty : value.ToString();
    }
    set
    {
        SetValue(IDProperty, value);
    }
}

/// <summary>
/// Property to set or return the glyph for this button.
/// </summary>
public ImageSource Glyph
{
    get
    {
        return GetValue(GlyphProperty) as ImageSource;
    }
    set
    {
        SetValue(GlyphProperty, value);
    }
}

/// <summary>
/// Property to set or return the tool tip for the button.
/// </summary>
public string ToolTip
{
    get
    {
        object value = GetValue(ToolTipProperty);

        return value == null ? string.Empty : value.ToString();
    }
    set
    {
        SetValue(ToolTipProperty, value);
    }
}

/// <summary>
/// Property to set or return whether the button is visible or not.
/// </summary>
public bool IsVisible
{
    get
    {
        return (bool)GetValue(IsVisibleProperty);
    }
    set
    {
        SetValue(IsVisibleProperty, value);
    }
}

/// <summary>
/// Property to set or return whether the button is enabled or not.
/// </summary>
public bool IsEnabled
{
    get
    {
        return (bool)GetValue(IsEnabledProperty);
    }
    set
    {
        SetValue(IsEnabledProperty, value);
    }
}
#endregion

#region Methods.
/// <summary>
/// Function to handle a change to the <see cref="GlyphProperty"/>.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void Glyph_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    // TODO
}

/// <summary>
/// Function to handle a change to the <see cref="IsVisibleProperty"/>.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void Visible_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    // TODO
}

/// <summary>
/// Function to handle a change to the <see cref="IsEnabledProperty"/>.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void Enabled_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    // TODO
}

/// <summary>
/// When implemented in a derived class, creates a new instance of the <see cref="T:System.Windows.Freezable" /> derived class.
/// </summary>
/// <returns>The new instance.</returns>
protected override Freezable CreateInstanceCore()
{
    return new MyToolbarButton();
}
#endregion

#region Constructor/Finalizer.
/// <summary>
/// Initializes a new instance of the <see cref="MyToolbarButton"/> class.
/// </summary>
/// <param name="buttonID">The ID of the button being clicked.</param>
/// <param name="defaultImageSource">The default image source URI for the glyph used by the button.</param>
internal MyToolbarButton(string buttonID, string defaultImageSource)
{
    ID = buttonID;
    IsVisible = true;
    IsEnabled = true;
    ToolTip = string.Empty;

    if (!string.IsNullOrWhiteSpace(defaultImageSource))
    {
        _defaultImageSource = new Uri(defaultImageSource, UriKind.Relative);
    }
}

/// <summary>
/// Initializes a new instance of the <see cref="MyToolbarButton"/> class.
/// </summary>
public MyToolbarButton()
{
    // This is here to keep the XAML designer from complaining.         
}
#endregion

但是,当我在XAML设计器中查看我的用户控件上的button属性,并展开其属性时,我得到了:

NoProperties

如您所见,XAML设计器中的该属性下没有属性。我想要的是让该按钮的属性出现在“FullExtentsButton”属性下,以便我的开发人员可以修改属性,但能够创建/删除已存在的实例。

我已经尝试在我的UserControl上创建一个DependencyProperty的FullExtentButton属性,但这并没有解决任何问题。

这是我们希望跨应用程序使用的标准工具栏的一部分,因此强制执行一致性对我们来说非常重要。此外,它将允许我们的开发人员专注于应用程序的其他部分,而不是一遍又一遍地重新实现相同的东西(这是我们现在必须做的)。

所以,那就是说,我在我的智慧结束,我做错了什么?

1 个答案:

答案 0 :(得分:0)

使用这个代码,我必须稍微改变一下才能编译....

/// <summary>
/// Interaction logic for MyToolBarButton.xaml
/// </summary>
public partial class MyToolBarButton : UserControl
{
    public MyToolBarButton()
    {
        InitializeComponent();
    }
      #region Dependency Properties.
    /// <summary>
    /// Dependency property for the <see cref="IsVisible"/> property.
    /// </summary>
    public static DependencyProperty IsVisibleProperty = DependencyProperty.Register("IsVisible", typeof(bool), typeof(MyOldToolBarButton),
                                                                                                 new FrameworkPropertyMetadata(true,
                                                                                                                           FrameworkPropertyMetadataOptions
                                                                                                                           .BindsTwoWayByDefault, Visible_Changed));
    /// <summary>
    /// Dependency property for the <see cref="IsEnabled"/> property.
    /// </summary>
    public static DependencyProperty IsEnabledProperty = DependencyProperty.Register("IsEnabled", typeof(bool), typeof(MyOldToolBarButton),
                                                                                     new FrameworkPropertyMetadata(true,
                                                                                                                   FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, Enabled_Changed));

    /// <summary>
    /// Dependency property for the <see cref="ToolTip"/> property.
    /// </summary>
    public static DependencyProperty ToolTipProperty = DependencyProperty.Register("ToolTip", typeof(string), typeof(MyOldToolBarButton),
                                                                                     new FrameworkPropertyMetadata(string.Empty,
                                                                                                                   FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                                                                                                                      new PropertyChangedCallback(ToolTipPropertyChanged)));

    private static void ToolTipPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        throw new NotImplementedException();
    }



    /// <summary>
    /// Dependency property for the <see cref="Glyph"/> property.
    /// </summary>
    public static DependencyProperty GlyphProperty = DependencyProperty.Register("Glyph", typeof(ImageSource), typeof(MyOldToolBarButton),
                                                                                     new FrameworkPropertyMetadata(null,
                                                                                                                   FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, Glyph_Changed));

    /// <summary>
    /// Dependency property for the <see cref="ID"/> property.
    /// </summary>
    public static DependencyProperty IDProperty = DependencyProperty.Register("ID", typeof(string), typeof(MyOldToolBarButton),
                                                                              new FrameworkPropertyMetadata(string.Empty,
                                                                                                            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    /// <summary>
    /// Dependency property for the <see cref="ClickedCommand"/> property.
    /// </summary>
    public static DependencyProperty ClickedCommandProperty = DependencyProperty.Register("ClickedCommand", typeof(string),
        typeof(MyOldToolBarButton),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
    #endregion

    #region Variables.
    // The default image source for the button glyph.
    public Uri _defaultImageSource { private set; get; }
    #endregion

    #region Properties.



    /// <summary>
    /// Property to set or return the ID of the button.
    /// </summary>

    [Category("Configuration")]
    public string ID
    {
        get
        {
            object value = GetValue(IDProperty);

            return value == null ? string.Empty : value.ToString();
        }
        set
        {
            SetValue(IDProperty, value);
        }
    }

    /// <summary>
    /// Property to set or return the glyph for this button.
    /// </summary>

    [Category("Configuration")]
    public ImageSource Glyph
    {
        get
        {
            return GetValue(GlyphProperty) as ImageSource;
        }
        set
        {
            SetValue(GlyphProperty, value);
        }
    }

    /// <summary>
    /// Property to set or return the tool tip for the button.
    /// </summary>
    /// 

    [Category("Configuration")]
    public string ToolTip
    {
        get
        {
            object value = GetValue(ToolTipProperty);

            return value == null ? string.Empty : value.ToString();
        }
        set
        {
            SetValue(ToolTipProperty, value);
        }
    }

    /// <summary>
    /// Property to set or return whether the button is visible or not.
    /// </summary>
    /// 
    [Category("Configuration")]
    public bool IsVisible
    {
        get
        {
            return (bool)GetValue(IsVisibleProperty);
        }
        set
        {
            SetValue(IsVisibleProperty, value);
        }
    }

    /// <summary>
    /// Property to set or return whether the button is enabled or not.
    /// </summary>
    /// 
    [Category("Configuration")]
    public bool IsEnabled
    {
        get
        {
            return (bool)GetValue(IsEnabledProperty);
        }
        set
        {
            SetValue(IsEnabledProperty, value);
        }
    }
    #endregion

    #region Methods.
    /// <summary>
    /// Function to handle a change to the <see cref="GlyphProperty"/>.
    /// </summary>
    /// <param name="sender">The sender of the event.</param>
    /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
    private static void Glyph_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        // TODO
    }

    /// <summary>
    /// Function to handle a change to the <see cref="IsVisibleProperty"/>.
    /// </summary>
    /// <param name="sender">The sender of the event.</param>
    /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
    private static void Visible_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        // TODO
    }

    /// <summary>
    /// Function to handle a change to the <see cref="IsEnabledProperty"/>.
    /// </summary>
    /// <param name="sender">The sender of the event.</param>
    /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
    private static void Enabled_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        // TODO
    }

    /// <summary>
    /// When implemented in a derived class, creates a new instance of the <see cref="T:System.Windows.Freezable" /> derived class.
    /// </summary>
    /// <returns>The new instance.</returns>
    protected  Freezable CreateInstanceCore()
    {
        return new MyOldToolBarButton();
    }
    #endregion

    #region Constructor/Finalizer.
    /// <summary>
    /// Initializes a new instance of the <see cref="MyOldToolBarButton"/> class.
    /// </summary>
    /// <param name="buttonID">The ID of the button being clicked.</param>
    /// <param name="defaultImageSource">The default image source URI for the glyph used by the button.</param>
    internal void MyOldToolBarButton(string buttonID, string defaultImageSource)
    {
        ID = buttonID;
        IsVisible = true;
        IsEnabled = true;
        ToolTip = string.Empty;

        if (!string.IsNullOrWhiteSpace(defaultImageSource))
        {
            _defaultImageSource = new Uri(defaultImageSource, UriKind.Relative);
        }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="MyOldToolBarButton"/> class.
    /// </summary>
    public void MyOldToolBarButton()
    {
        // This is here to keep the XAML designer from complaining.         
    }
    #endregion
}

并将其添加到另一个“父”控件......属性如下所示:

Properties

这是你在找什么?