我有一個簡單的MVVM WPF應用程序與數據庫優先EF dbContext(這是Global.Database
在我的應用程序),這是長期居住在我的應用程序。 我有一個窗口與列表框ItemsSource
綁定到視圖模型屬性Clients
這是我的dbmodel Client
ObservableCollection
。WPF實體框架刷新一個上下文實體
該列表框的SelectedItem
被綁定到名爲SelectedClient
的視圖模型屬性。
Client
實體類有一個字段叫last_status
這是一個簡單的int從我的數據庫。
所以,在我看來,當我從列表框中選擇客戶端時,綁定到SelectedClient的last_status
的標籤應該顯示值last_status
。
我在viewmodel中添加了一個按鈕和一個刷新命令。我想要的是:當我手動更改我的數據庫中客戶端的last_status
,並在我的視圖中按刷新按鈕時,標籤的內容應該改變。但我絕對不知道如何實現這一目標。這裏是我的視圖模型代碼的部分(我使用Catel,但不要緊,這種情況下):
public ClientManagerWindowViewModel()
{
RefreshClientInfoCommand = new Command(OnRefreshClientInfoCommandExecute);
Clients = new ObservableCollection<Client>();
RefreshClients();
}
public ObservableCollection<Client> Clients
{
get { return GetValue<ObservableCollection<Client>>(ClientsProperty); }
set { SetValue(ClientsProperty, value); }
}
public static readonly PropertyData ClientsProperty = RegisterProperty("Clients", typeof(ObservableCollection<Client>));
public Client SelectedClient
{
get
{return GetValue<Client>(SelectedClientProperty);}
set
{
SetValue(SelectedClientProperty, value);
}
}
public static readonly PropertyData SelectedClientProperty = RegisterProperty("SelectedClient", typeof(Client));
//here is my refresh button command handler:
public Command RefreshClientInfoCommand { get; private set; }
private void OnRefreshClientInfoCommandExecute()
{
RefreshClientInfo(SelectedClient);
}
//and here is my "logic" for working with dbcontext:
private void RefreshClients()
{
var qry = (from c in Global.Database.Clients where c.client_id != 1 select c).ToList();
Clients = new ObservableCollection<Client>(qry);
}
private void RefreshClientInfo(Client client)
{
Global.Database.Entry(client).Reload();
}
我的XAML的列表框:
<ListBox
x:Name="ClientsListBox"
Grid.Row="1"
Margin="5"
DisplayMemberPath="fullDomainName"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Clients}"
SelectedItem="{Binding SelectedClient}" />
我的XAML一個標籤:
<Label Margin="5" Content="{Binding SelectedClient.last_status}" />
而對於一個按鈕:
<Button Command="{Binding RefreshClientInfoCommand}" Content="↻"/>
現在,當我在數據庫中手動更改客戶端的last_status
值並按刷新按鈕時,什麼都不會發生。但是,當我在列表框中選擇另一個客戶端,然後返回到所需的客戶端 - 標籤內容正確更新。我知道,也許我錯過了一些非常愚蠢和簡單的事情,但我無法弄清楚究竟是什麼。也許我需要在我的按鈕命令處理程序中強制更改SelectedClient
,或者以某種方式致電SelectedClient
的設置程序... 請幫助我。非常感謝。
你也應該刷新綁定。 –