我是WPF和MVVM的新手,所以對我很感興趣。使用WPF驗證命令
基本上,我正在創建一個應用程序以使用戶能夠將他們的組織詳細信息輸入到數據庫中。
我創造我的WPF應用程序中,我有一個View Model
包含properties
,commands
和entity framework
方法(我知道這是不使用MVVM正確的方式,但我慢慢學會如何去實現它) 。
在我的觀點之一,它是一個選項卡控件,允許用戶輸入不同的組織詳細信息到數據庫中,然後,在另一個視圖上,我有一個數據網格來顯示他們輸入的內容以使用戶在需要時更新內容。
這導致我的問題。到目前爲止,我已經驗證了我的命令,以便當某些字段爲空時,該按鈕將不會被激活,但是一旦它們被輸入,它們將被激活。像這樣;
private ICommand showAddCommand;
public ICommand ShowAddCommand
{
get
{
if (this.showAddCommand == null)
{
this.showAddCommand = new RelayCommand(this.SaveFormExecute, this.SaveFormCanExecute);//i => this.InsertOrganisation()
}
return this.showAddCommand;
}
}
private bool SaveFormCanExecute()
{
return !string.IsNullOrEmpty(OrganisationName) && !string.IsNullOrEmpty(Address) && !string.IsNullOrEmpty(Country) && !string.IsNullOrEmpty(Postcode)
&& !string.IsNullOrEmpty(PhoneNumber) && !string.IsNullOrEmpty(MobileNumber) && !string.IsNullOrEmpty(PracticeStructure) && !string.IsNullOrEmpty(PracticeType) && !string.IsNullOrEmpty(RegistrationNumber);
}
private void SaveFormExecute()
{
InsertOrganisations();
}
Xaml:
<Button Content="Save" Grid.Column="1" Grid.Row="18" x:Name="btnSave" VerticalAlignment="Bottom" Width="75" Command="{Binding ShowAddCommand}"/>
但我希望能實現的是,一旦用戶已經進入了1個組織到數據庫中,那麼該命令不被激活幹脆防止用戶意外進入另一個組織。目的是隻允許添加1個組織,不多也不少。
這可能嗎?
不知道這是你在找什麼,所以我把它作爲評論發佈。我認爲你需要的是每個表單驗證。看看這篇文章(http://www.scottlogic.co.uk/blog/colin/2009/01/bindinggroups-for-total-view-validation/)。 – dowhilefor
感謝您的回覆。那麼,我的領域已經驗證使用屬性驗證,所以這不是問題。我只想要一種能夠驗證我的命令的方法,因此它只允許輸入一行數據(在數據庫中),可能使用標誌?但我不知道如何實現這一點。 –
爲什麼不將IsEnabled屬性綁定到您調用的屬性bool HasOrganisation {return mOrganisation.Count> 0;}如果組織屬性發生更改,則引發此問題。它很難給你一個很好的答案,沒有關於你的代碼的更多信息。最好給我們一個你想做什麼的非常小的可運行的例子。 – dowhilefor