我跟着this guide将WinForms Windows媒体播放器导入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;
}
但是从WPF我得到了:A&#39; Binding&#39;不能在&#39; URL&#39;上设置类型&#39; AxWindowsMediaPlayer&#39;的属性。 A&#39;绑定&#39;只能在DependencyObject的DependencyProperty上设置。