프리즘 프레임 워크를 사용하여 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
}
}
표시 할 코드가 INotifyPropertyChanged를 구현하지 않습니다. 제공된 코드가 아닌 것으로 가정합니다. BindableBase에서 코드를 추가 할 수 있습니까? – MikeT
모델 클래스의'Start()'메소드를 호출하고 새로운 값인 'new label'이 올지 여부를 확인할 때'CurrentOperationLabel'의 setter에 중단 점을 설정하십시오. – StepUp
나의 초기 의심은 당신의 시작 방법이 viewModel이 아닌 모델을 변경하여 View 모델이 변경된 것을 알 수 없으므로 통지 할 수 없다는 것입니다. – MikeT