妳理解我寫的代碼嗎?
我只是用我自己的cs文件作為虛擬機。....
關鍵是這個。DataContext = this這句話...
這個。DataContext = this
↑ ↑
這這是壹張圖片,這這實際上是壹個vm,只是不想寫另壹個類。....
仔細看看標準的MVVM。...
如果我改變它妳會明白的。
事實上,妳解決這個問題的方法很簡單。
這是在獲得模糊查詢的結果之後。
1將結果集的計數值支付給綁定標簽的屬性。內容屬性(我這裏有SelectedCount屬性)。您的模糊查詢在虛擬機中完成,該屬性當然也在虛擬機中...這是必要的。...
然後調用inotifypropertychanged。屬性已更改以通知屏幕更新。
對了,應該是程序對屏幕的更新,所以綁定模式應該是單向的
-
修改後:
在Xaml文件中:
& ltlabel Content =“{ Binding selected count,Mode = one way }“/& gt;
在vm.cs文件中:
公共分部類視圖模型:INotifyPropertyChanged
{
公共事件PropertyChangedEventHandler property changed;
公共視圖模型()
{
}
private int _ selectedCount
公共int SelectedCount
{
get { return _ selectedCount}
設置
{
_ selectedCount = value
NotifyPropertyChanged(“selected count“);
}
}
私有void NotifyPropertyChanged(字符串信息)
{
if(property changed!=空)
{
property changed(this,new PropertyChangedEventArgs(info));
}
}
私有數據表select data(string _ where)
{
//下面是妳的模糊查詢方法。
DataTable table = mySelector。select(_ where);
//在此為標簽賦值。
SelectedCount = table。數數;
//返回綁定網格的結果集。
返回表
}
}
-
修改前:
在Xaml文件中:
& ltlabel Content =“{ Binding selected count,Mode = two way }“/& gt。
在cs文件中:
公共分部類MainWindow:Window,INotifyPropertyChanged
{
公共事件PropertyChangedEventHandler property changed;
公共主窗口()
{
這個。DataContext = this
//在這裏設置條件後查詢,找到幾項。
DataTable table = mySelector。select(“mySQL“);
int _count =表。行數。計數;
//在此為標簽賦值。
SelectedCount = _ count
}
private int _ selectedCount
公共int SelectedCount
{
get { return _ selectedCount}
設置
{
_ selectedCount = value
NotifyPropertyChanged(“selected count“);
}
}
私有void NotifyPropertyChanged(字符串信息)
{
if(property changed!=空)
{
property changed(this,new PropertyChangedEventArgs(info));
}
}
}
只點擊了鍵碼。