C# 如何向WInForms UserControl添加DependencyProperty以在WPF中绑定

C# 如何向WInForms UserControl添加DependencyProperty以在WPF中绑定,c#,wpf,winforms,dependency-properties,axwindowsmediaplayer,C#,Wpf,Winforms,Dependency Properties,Axwindowsmediaplayer,我随后将WinForms windows media player导入WPF 现在我想在我的WPF应用程序中绑定控件的URL属性。但是,当我尝试这样做时,我会得到一个错误,因为控件不是从DependencyObject类继承的 我尝试将URL注册为DependencyProperty,如下所示: partial class WMPControl { /// <summary> /// Required designer variable. /// </s

我随后将WinForms windows media player导入WPF

现在我想在我的WPF应用程序中绑定控件的URL属性。但是,当我尝试这样做时,我会得到一个错误,因为控件不是从DependencyObject类继承的

我尝试将URL注册为DependencyProperty,如下所示:

partial class WMPControl
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WMPControl));
        this.axWindowsMediaPlayer1 = new AxWMPLib.AxWindowsMediaPlayer();
        ((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).BeginInit();
        this.SuspendLayout();
        // 
        // axWindowsMediaPlayer1
        // 
        this.axWindowsMediaPlayer1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.axWindowsMediaPlayer1.Enabled = true;
        this.axWindowsMediaPlayer1.Location = new System.Drawing.Point(0, 0);
        this.axWindowsMediaPlayer1.Name = "axWindowsMediaPlayer1";
        this.axWindowsMediaPlayer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWindowsMediaPlayer1.OcxState")));
        this.axWindowsMediaPlayer1.Size = new System.Drawing.Size(634, 437);
        this.axWindowsMediaPlayer1.TabIndex = 0;
        // 
        // UserControl1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(this.axWindowsMediaPlayer1);
        this.Name = "UserControl1";
        this.Size = new System.Drawing.Size(634, 437);
        ((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    public static readonly DependencyProperty MyCustomProperty =
    DependencyProperty.Register("URL", typeof(string), typeof(AxWMPLib.AxWindowsMediaPlayer));


    private AxWMPLib.AxWindowsMediaPlayer axWindowsMediaPlayer1;
}
部分类WMP控制
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域组件设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager资源=新的System.ComponentModel.ComponentResourceManager(类型(WMPControl));
this.axWindowsMediaPlayer1=新的AxWMPLib.AxWindowsMediaPlayer();
((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).BeginInit();
这个.SuspendLayout();
// 
//axWindowsMediaPlayer1
// 
this.axWindowsMediaPlayer1.Dock=System.Windows.Forms.DockStyle.Fill;
this.axWindowsMediaPlayer1.Enabled=true;
this.axWindowsMediaPlayer1.Location=新系统.Drawing.Point(0,0);
this.axWindowsMediaPlayer1.Name=“axWindowsMediaPlayer1”;
this.axWindowsMediaPlayer1.OcxState=((System.Windows.Forms.AxHost.State)(resources.GetObject(“axWindowsMediaPlayer1.OcxState”));
this.axWindowsMediaPlayer1.Size=新系统.Drawing.Size(634437);
this.axWindowsMediaPlayer1.TabIndex=0;
// 
//用户控制1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.axWindowsMediaPlayer1);
this.Name=“UserControl1”;
该尺寸=新系统图纸尺寸(634437);
((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).EndInit();
此选项为.resume布局(false);
}
#端区
公共静态只读从属属性MyCustomProperty=
DependencyProperty.Register(“URL”、typeof(字符串)、typeof(AxWMPLib.AxWindowsMediaPlayer));
私有AxWMPLib.AxWindowsMediaPlayer axWindowsMediaPlayer1;
}

但从WPF中我得到:不能在“AxWindowsMediaPlayer”类型的“URL”属性上设置“绑定”。“绑定”只能在DependencyObject的DependencyProperty上设置。

这意味着WMPControl应该从DependencyObject继承才能工作。另外:看看下面:如何创建DependencyProperty如果没有更多的上下文,包括一个清楚说明您的场景的好例子,就不可能确定您的情况下最好的答案是什么。但是,在我看来,您最好编写一个WPF
UserControl
,它已经是一个
DependencyObject
(实现dependency属性所需满足的要求),您可以直接在
WindowsFormsHost
对象中托管WPM ActiveX控件,而不是添加Winforms
UserControl
的额外层。另外,请考虑使用<代码>媒体元素< /代码>。