2017-02-24 4 views
0

내 응용 프로그램에 오래 살아있는 데이터베이스 우선 EF dbContext (내 응용 프로그램에서는 Global.Database)가있는 간단한 MVVM WPF 응용 프로그램이 있습니다. ItemsSource이라는 목록 상자에 Clients이라는이라는 뷰 모델 속성에 바인드 된 창이 있습니다. 내 dbmodel은 Client입니다.WPF Entity Framework가 하나의 컨텍스트 엔터티를 새로 고칩니다.

이 목록 상자의 SelectedItemSelectedClient이라는 viewmodel 속성에 바인딩됩니다.

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 :

<Button Command="{Binding RefreshClientInfoCommand}" Content="↻"/> 
:
<Label Margin="5" Content="{Binding SelectedClient.last_status}" /> 

그리고 버튼 지금은 데이터베이스에서 클라이언트의 last_status 값을 수동으로 변경하고 새로 고침 버튼을 누르면 아무 일도 발생하지 않습니다. 그러나 목록 상자에서 다른 클라이언트를 선택한 다음 필요한 클라이언트 레이블 내용 업데이트로 올바르게 돌아갈 때. 나는 어쩌면 내가 어리 석고 단순한 무언가를 놓친다는 것을 알지만, 정확히 무엇을 알아낼 수는없는 것이다. 어쩌면 내 버튼 명령 처리기에서 변경 SelectedClient을 강제로 호출하거나 SelectedClient setter를 호출해야합니다. 제발, 도와주세요. 고마워.

+0

바인딩을 새로 고쳐야합니다. –

답변

1

글쎄, 난 내 코드를 잘못 알아 냈어.

내 databindig는 SelectedClient.last_status으로 설정되었습니다. 어떤 이유로 그것은 예상대로 작동하지 않았다.그래서 나는 LastStatus라는 새로운 뷰 모델 속성을 만들어 내 RefreshClientInfo 수정 :이 새 속성에

private void RefreshClientInfo(Client client) 
    { 
     Global.Database.Entry(client).Reload(); 
     LastStatus = client.last_status; 
     SetValue(SelectedClientProperty, client); 
    } 

및 바인더 제본 라벨을. 이제 모든 것이 올바로 작동합니다.

0

SelectedClient 속성을 EF 쿼리에서 다시 가져온 Client 개체로 설정해야합니다.

데이터베이스를 쿼리하기 전에 client_id을 저장하고이 client_id 인 새로운 Client 개체를 선택하여이 작업을 수행 할 수 있습니다.

이 시도 :

private void RefreshClients() 
{ 
    int? currentlySelectedClientId = (SelectedClient != null) ? SelectedClient.client_id : default(int?); 
    var qry = (from c in Global.Database.Clients where c.client_id != 1 select c).ToList(); 
    Clients = new ObservableCollection<Client>(qry); 
    if (currentlySelectedClientId.HasValue) 
     SelectedClient = Clients.FirstOrDefault(x => x.client_id = currentlySelectedClientId.Value); 
} 

편집 : 당신이 DB에서 업데이트 된 기록을 가져 있는지 확인하십시오

private void RefreshClientInfo(Client client) 
{ 
    var newClient = (from c in Global.Database.Clients where c.client_id == client.client_id select c).ToList(); 
    SetValue(SelectedClientProperty, newClient[0]); 
} 
+0

죄송합니다. 어떻게 도움이 될지 모르겠습니다. 모든 클라이언트를 새로 고치고 싶지는 않습니다. 단 하나만 새로 고침하고 싶습니다. 그리고 내가 일반적으로 요구하는 함수는'RefreshClientInfo (클라이언트 클라이언트)'이며,'RefreshClients()'는 아니다. 하지만 어쩌면 내가 뭔가를 이해하지 못할 수도 있습니다. –

+0

업데이트 한 후에 데이터베이스에서 클라이언트를 다시 가져와야합니다. 내 편집을 참조하십시오. 내가 버튼 :(그것은 내가 목록 상자에서 클라이언트를 선택하고 다시 돌아 변경할 때 새로 고침을 누르면 – mm8

+0

은 여전히 ​​당신이 데이터베이스에서 업데이트 된 항목을 가져올 수 있는지 확인해야합니다 –