[英]Binding Property to 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
...
我也有一個DataClass:
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
}
基礎對象用於自動創建依賴性屬性:
[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的設置不會發生任何變化。 我忽略了什么嗎?
您是否在輸出窗口中查看過? 通常,如果綁定無提示地失敗,則您會在其中看到一些錯誤。
您需要檢查什么是傳遞到PicSelection控件中的數據上下文。 您可以嘗試顯式設置數據上下文,然后直接綁定到Images屬性嗎?
LstImages="{Binding Path=Images}"
如果Data
實例是UserControl
的DataContext
,則需要將Binding
更新為
<controls:PicSelection Name="SelPic" LstImages="{Binding Images}" Foreground="White" FontSize="16"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.