[英]NullReferenceException Query SQLite database with Where on a concatenated string property
[英]NullReferenceException on String Property
在我的程序中,我允許用戶添加到綁定到TreeView
的數據集合。 用戶創建新節點時,可以為其分配名稱。 用戶將節點名稱寫在位於彈出窗口中的textBox
。 文本框中的文本綁定到ViewModel中的屬性。
但是,我似乎無法在ViewModel中獲取屬性或string
來讀取綁定到的值。 它只是在調試器中顯示為null
,這是我得到異常的時間。 我已經創建了其他完全像這樣的窗口,但是由於某種原因,這個窗口不想工作。
XAML:
<TextBox Text="{Binding TransName}" MaxHeight="20" MinHeight="20" Height="20" Margin="142,24,12,40" Name="textBox1" TextChanged="textBox1_TextChanged"></TextBox>
在視圖模型中:
private string _nodeName;
//Property for Node Name
public string NodeName
{
get { return _nodeName; }
set
{
_nodeName = value;
NotifyPropertyChange(() => NodeName);
}
}
仍然在視圖模型中,這是發生異常的行
//hasSpace is a boolean
hasSpace = _nodeName.Contains(" ");
一種可能的解決方案是使用屬性而不是后備字段。 這將控制可以返回哪種輸出,並消除NullReferenceException
。
更改屬性以默認返回空。
public string NodeName
{
get { return _nodeName ?? string.Empty; }
set
{
_nodeName = value;
NotifyPropertyChange(() => NodeName);
}
}
然后將ViewModel更改為使用該屬性:
hasSpace = NodeName.Contains(" ");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.