![](/img/trans.png)
[英]The expected type was 'System.Nullable`1[System.Guid]' but the actual value was null
[英]Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Nullable`1[System.Guid]'
與此處詢問的問題類似,但在這種情況下,導致此異常的原因似乎有所不同。 也許我設置的財產有誤?
我有一個要綁定的自定義用戶控件。 但是,InitializeComponent引發錯誤Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Nullable 1[System.Guid]'
VS表示引發此錯誤的是set屬性。
我簡化了屬性以嘗試查找錯誤,但是在沒有任何設置邏輯的情況下仍然會發生
public Guid? SelectedItemGUID
{
get
{
if (SelectedItem == null) return null;
else return (Guid)SelectedItem.GetType().GetProperty("GUID").GetValue(SelectedItem, null);
}
set
{
return;
}
}
我正在使用(client.BIllToClient是Guid?
SelectedItemGUID="{Binding Path=client.BillToClient, Mode=TwoWay}"
嘗試這個
public static readonly DependencyProperty SelectedItemGUIDProperty = DependencyProperty.Register("SelectedItemGUID",typeof(Guid?),typeof(UserControl),new FrameworkPropertyMetadata(null));
public Guid? SelectedItemGUID
{
public Guid? SelectedItemGUID
{
get { return (Guid?)GetValue(SelectedItemGUIDProperty); }
set { SetValue(SelectedItemGUIDProperty, (Guid?)SelectedItem.GetType().GetProperty("GUID").GetValue(SelectedItem, null)); }
}
}
我認為您正在嘗試綁定簡單的CLR屬性。 綁定需要Target作為DependencyProperty 自定義綁定 。在上面的代碼中,我將typeof(UserControl)作為父類型替換為控件的Type。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.