如何通过 VM 方法根据 属性 的值变化更新视图元素显示?

How to update view element display based on property`s value change by VM method?

需要一些帮助。大家好。我真的不知道如何更新视图元素,当 属性 值用我在 MsgViewModel 内的 Egzecute 方法更新时,用 public ICommand Start 调用。例如,当 属性 Status 将其值从 Stopped 更改为 [= 时,我想让一个按钮 STOP 可见,另一个 START 折叠22=]。另请注意,当 属性 Status 使用 ViewModels 构造函数更改时,可见性会正确更新(对我来说默认为启动时)Status = Models.SendingStatus.Stopped;Status = Models.SendingStatus.Sending;

查看:

<!--START, to be collapsed-->
<Button Grid.Row="0"
                Grid.Column="4"
                Background="#80B584"
                Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Mode=OneWay,
            Converter={StaticResource boolStart}}" Margin="0,145,443.667,-0.333"
                Command="{Binding Path=Start}">
            <TextBlock Text="START" TextWrapping="Wrap" TextAlignment="Center"/>
        </Button>
        <!--STOP, to be viewed-->
        <Button Grid.Row="0"
                Background="#FF8A8A"
                Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Mode=OneWay,
            Converter={StaticResource boolStop}}" Margin="0,145,443.667,-0.333">
            <TextBlock Text="STOP" TextWrapping="Wrap" TextAlignment="Center"/>
        </Button>

视图模型:

private Models.MsgModel message= new Models.MsgModel (); //model instance
public MsgViewModel() //constructor, by default makes staus "Stopped"
        {
            Status = Models.SendingStatus.Stopped;
        }
public Models.SendingStatus Status
        {
            get
            {
                return message.Status;
            }
            set
            {
                message.Status = value;
            }
        }
private ICommand start;
        public ICommand Start //command called by START button, supposed to collapse it, and show STOP button
        {
            get
            {
                if (start == null)
                    start = new RelayCommand(
                    o =>
                    {
                        Egzecute();
                    });
                return start;
            }
        }
public void Egzecute() //method called by the command
        {
            Status = Models.SendingStatus.Sending;
            var openDialog = new Powiadomienie();
            openDialog.ShowPowiadomienie(Status.ToString(), "Powiadomienie"); //shows updated SendingStatus, but the View is not updating to it
        }

型号:

public enum SendingStatus: byte { Sending, Waiting, Stopped} //enum for Status property
public class MsgModel : INotifyPropertyChanged
private SendingStatus status;
        public SendingStatus Status //Status model property
        {
            get
            {
                return status;
            }
            set
            {
                status = value;
                OnPropertyChanged("Status");
            }
        }
public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(params string[] propertyNames) 



        {
            if (PropertyChanged != null)
            {
                foreach (string propertyName in propertyNames) 
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

转换器:

public class BooleanStart : IValueConverter //text decoration
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ViewModels.MsgViewModel mvm = new ViewModels.MsgViewModel();
            bool bvalue = (bool)value;
            if (mvm.Status == Models.SendingStatus.Sending|| mvm.Status == Models.SendingStatus.Waiting)
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter,
        CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class BooleanStop : IValueConverter //text decoration
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ViewModels.MsgViewModel mvm = new ViewModels.MsgViewModel();
            bool bvalue = (bool)value;
            if (mvm.Status == Models.SendingStatus.Sending|| mvm.Status == Models.SendingStatus.Waiting)
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter,
        CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

我的问题是,通过命令调用方法后如何更新View?

好的,几个小时后我发现了我的错误。 Converter 的构造是错误的。绑定应该是不同的,并且 ViewModel 更新了 属性 更改通知。 转换器:

public class BooleanStart : IValueConverter //text decoration
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Models.SendingStatus sendingStatus = (Models.SendingStatus)value;
            if (sendingStatus == Models.SendingStatus.Sending || sendingStatus == Models.SendingStatus.Waiting)
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter,
        CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class BooleanStop : IValueConverter //text decoration
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Models.SendingStatus sendingStatus = (Models.SendingStatus)value;
            if (sendingStatus == Models.SendingStatus.Sending || sendingStatus == Models.SendingStatus.Waiting)
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter,
        CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

绑定:

<!--START-->
        <Button Grid.Row="0"
                Grid.Column="4"
                Background="#80B584"
                Visibility="{Binding Path=Status, Converter={StaticResource boolStart}}" Margin="0,145,443.667,-0.333"
                Command="{Binding Path=Start}">
            <TextBlock Text="START" TextWrapping="Wrap" TextAlignment="Center"/>
        </Button>
        <!--STOP-->
        <Button Grid.Row="0"
                Background="#FF8A8A"
                Visibility="{Binding Path=Status, Converter={StaticResource boolStop}}" Margin="0,145,443.667,-0.333"
                Command="{Binding Path=Start}">
            <TextBlock Text="STOP" TextWrapping="Wrap" TextAlignment="Center"/>
        </Button>

ViewModel` 方法:

public void Egzecue()
        {
            Status = Models.SendingStatus.Sending;
            OnPropertyChanged("Status");
            var openDialog = new Powiadomienie();
            openDialog.ShowPowiadomienie(Status.ToString(), "Powiadomienie");
        }