简体   繁体   English

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

[英]WPF button property on user control not showing properties in property pane

I've been beating my head against the wall over this for days now and I can't seem to find any info that fits my problem. 几天来,我一直在头上碰壁,似乎找不到适合我的问题的任何信息。

So, I have this toolbar Usercontrol that's meant to be dropped in to an application. 因此,我有这个工具栏Usercontrol,可以将其放入应用程序中。 This toolbar has a property exposed called "FullExtentButton" which is a reference to the button. 该工具栏具有公开的名为“ FullExtentButton”的属性,该属性是对该按钮的引用。 What I want is to expose the properties of this button in the designer properties pane on the toolbar user control so the developers can set the properties directly from the designer. 我想要的是在工具栏用户控件的设计器属性窗格中公开此按钮的属性,以便开发人员可以直接从设计器设置属性。

In WinForms, this was very easy to do. 在WinForms中,这很容易做到。 WPF, not so much (unless I'm just blind). WPF,不是很多(除非我只是盲目的)。

In my tool bar code: 在我的工具条形码中:

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

This value is set in the constructor the user control: 此值是在用户控件的构造函数中设置的:

public MyToolbar()
{
    InitializeComponent();

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

The button itself is quite simple: 该按钮本身非常简单:

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

But, when I view the button property on my user control in the XAML designer, and expand its properties I get this: 但是,当我在XAML设计器中查看用户控件上的button属性并扩展其属性时,会得到以下信息:

没有属性

As you can see, there are no properties under that property in the XAML designer. 如您所见,XAML设计器中该属性下没有任何属性。 What I want is to have the properties for that button appear under the "FullExtentsButton" property so that my developers can modify the properties, but not be able to create/remove the instance that's already there. 我想要的是使该按钮的属性出现在“ FullExtentsButton”属性下,以便我的开发人员可以修改属性,但不能创建/删除已经存在的实例。

I've tried making the FullExtentButton property on my UserControl a DependencyProperty, but that didn't fix anything. 我试图将UserControl上的FullExtentButton属性设置为DependencyProperty,但这并不能解决任何问题。

This is part of a standard toolbar that we want to use across applications, so enforcing consistency is pretty important for us. 这是我们要在各个应用程序中使用的标准工具栏的一部分,因此强制执行一致性对我们来说非常重要。 Plus it will allow our devs to focus on other parts of applications rather than reimplementing the same thing over and over (which is what we're having to do right now). 另外,它将使我们的开发人员可以专注于应用程序的其他部分,而不是一遍又一遍地重新实现同一件事(这是我们现在要做的事情)。

So, that said, I'm at my wits end here, what am I doing wrong? 所以,就是说,我在这里才智,我在做什么错?

Using this code which I had to change somewhat to get it to compile.... 使用此代码,我必须对其进行一些更改才能进行编译。

/// <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
}

And adding it to another "parent" control... The properties look like this: 并将其添加到另一个“父”控件中...属性如下所示:

性质

Is this what you are looking for? 这是你想要的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM