2016-07-13 6 views
0

프리즘 프레임 워크를 사용하여 MVVM 응용 프로그램을 작성하고 있습니다. 속성 값이 변경되면 내 라벨을 업데이트 할 수 없습니다. 모델을 만들고 초기 값을 Property에 할당하면 바인딩 된 lable이 업데이트됩니다. 그러나 응용 프로그램 수명 동안 속성을 변경하면 레이블의 내용이 업데이트되지 않습니다. 명령이 호출됩니다 "시작"때 바인딩 된 VM 속성이 변경되면 WPF MVVM 컨트롤이 업데이트되지 않습니다.

public class MyModelViewModel : BindableBase 
{ 
    private MyModel model; 
    private string currentOpeartion; 

    public DelegateCommand Start { get; private set; } 

    public string CurrentOperationLabel 
    { 
     get { return currentOpeartion; } 
     set { SetProperty(ref currentOpeartion, value); } 
    } 

    public MyModelViewModel() 
    { 
     model = new MyModel(); 

     Start = new DelegateCommand (model.Start); 
     CurrentOperationLabel = model.CurrentOperation; //Bind model to the ViewModel 
    } 
} 

그리고 내 모델에서

, 내가 레이블을 변경 :

<Window x:Class="Project.Views.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="700" Width="700"> 
    <DockPanel LastChildFill="True"> 
      <Button x:Name="btnStart" Command="{Binding Path=Start}" Content="StartProcess"/> 

      <GroupBox Header="Current Operation"> 
       <Label x:Name="lblCurrentOperation" Content="{ Binding CurrentOperationLabel, UpdateSourceTrigger=PropertyChanged}"/> 
      </GroupBox> 
    </DockPanel> 
</Window> 

여기 내 뷰 모델입니다 :

여기 내 XAML입니다.

public class MyModel 
{ 
    public string CurrentOperation { get; set; } 

    public MyModel() 
    { 
     CurrentOperation = "aaa"; //This will make the label show "aaa" 
    } 

    public void Start() 
    { 
     CurrentOperation = "new label"; //This should alter the Label in the view, but it doesn't 
    } 
} 
+1

표시 할 코드가 INotifyPropertyChanged를 구현하지 않습니다. 제공된 코드가 아닌 것으로 가정합니다. BindableBase에서 코드를 추가 할 수 있습니까? – MikeT

+0

모델 클래스의'Start()'메소드를 호출하고 새로운 값인 'new label'이 올지 여부를 확인할 때'CurrentOperationLabel'의 setter에 중단 점을 설정하십시오. – StepUp

+0

나의 초기 의심은 당신의 시작 방법이 viewModel이 아닌 모델을 변경하여 View 모델이 변경된 것을 알 수 없으므로 통지 할 수 없다는 것입니다. – MikeT

답변

5

문제 Start 방법으로는 모델 (즉 CurrentOperation)가 아닌 뷰 모델 (즉 CurrentOperationLabel)의 속성의 속성을 수정하게된다. XAML은 뷰 모델에 바인딩되어 있으므로 모델에 대해 아무것도 모릅니다. 즉, MyModel.CurrentOperation 속성을 수정하면 XAML에이 사실이 통보되지 않습니다.

이 문제를 해결하려면 코드 구조를 변경해야합니다. 모델을 업데이트 한 후에 뷰 모델을 새로 고침해야합니다. 나의 제안은이 방법으로 MyModelViewModel을 수정하는 것입니다 :

public class MyModelViewModel : BindableBase 
{ 
     //... 

     public void InnerStart() 
     { 
      model.Start(); 
      //Refresh the view model from the model 
     } 

     public MyModelViewModel() 
     { 
     model = new MyModel(); 

     Start = InnerStart; 
     CurrentOperationLabel = model.CurrentOperation; 
    } 
} 

아이디어는 버튼의 클릭은 모델과의 통신을 담당하는 뷰 모델에서 처리해야한다는 것입니다. 또한 모델의 현재 상태에 따라 그에 따라 속성을 업데이트합니다.

+0

네, 요점이 있습니다. 그러면 모델의 속성이 변경된 ViewModel에 어떻게 알릴 수 있습니까? 당신이 내 코드를 고쳤다면 나는 괜찮을 것이다. :) – Zwierzak

관련 문제