我得到了一個包含類型列表的依賴屬性的usercontrol(它位於一個庫中/也試過一個正常的屬性)。綁定屬性到usercontrol
public partial class PicSelection : UserControl
{
#region Properties
public static readonly DependencyProperty LstImagesProperty = DependencyProperty.Register("LstImages", typeof(List<string>), typeof(PicSelection), new FrameworkPropertyMetadata(null));
// .NET Property wrapper
public List<string> LstImages
{
get { return (List<string>)GetValue(LstImagesProperty); }
set { SetValue(LstImagesProperty, value); }
}
#endregion
...
我也得到了數據類:
public class Data : BaseObject
{
#region Members
public List<string> Images { set { SetValue("Images", value); } get { return (GetValue<List<string>>("Images")); } }
#endregion
#region Construction
public GameData()
{
Images = new List<string>();
Images.Add("pack://application:,,,/TestApp;component/Content/Images/Pictures/0002.jpg");
}
#endregion
}
基礎對象用於自動創建Dependance的屬性:
[Serializable]
public abstract class BaseObject : PropertyNotifier
{
#region Members
private readonly IDictionary<string, object> _values = new Dictionary<string, object>(StringComparer.CurrentCultureIgnoreCase);
#endregion
現在我想的Data.Images綁定到customcontrol。 LstImages(「Data」是使用控件的頁面上的Data數據類型的屬性)。該程序毫無例外地工作,但不知何故在控制中的LstImages,我檢查了幾個事件,始終爲空。
<controls:PicSelection Name="SelPic" LstImages="{Binding Data.Images}" Foreground="White" FontSize="16"/>
。另一方面,要做到每
<usercontrol SomeArray="{x:Static data:StaticClass.TheStrings}"/>
與靜態類(涉及組織,幾乎是相同的),同樣的事情,就是這麼簡單。它甚至可以與普通屬性一起使用Datacontext的設置對此沒有任何影響。我忽略了什麼?
什麼在Visual Studio輸出窗口的?你應該在那裏看到綁定錯誤。 – 2013-11-01 12:05:53