mvvm - 属性值不持久 [英] mvvm - property value not persisting

查看:53
本文介绍了mvvm - 属性值不持久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

========================================

========================================

xaml

=============================== =========

========================================

推荐答案

您好OldeEnglishD,

Hi OldeEnglishD,

我只是试图测试项目中的代码以查看错误,但我发现有更多错误与您提出的问题无关,请您发布我们可以运行的代码:)

I just tried to test the code in a project to see what's wrong, but I see that there is more error not related to the question you ask, can you please post a code that we can run :)

MainWindow.xaml

<Window x:Class="FTPFileMaintWPF_MVVM.Views.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
              xmlns:command="http://www.galasoft.ch/mvvmlight"
              xmlns:catel="http://schemas.catelproject.com"
              xmlns:local="clr-namespace:FTPFileMaintWPF_MVVM"
      xmlns:od="clr-namespace:Gat.Controls;assembly=Gat.Controls.OpenDialog"
              xmlns:viewmodel="clr-namespace:FTPFileMaintWPF_MVVM.ViewModels"  
              ResizeMode="CanResize"
              Height="1050" Width="800">
    <Window.Resources>
        <local:Database2 x:Key="db2" />
        <local:Server x:Key="sv" />
        <viewmodel:MainWindowViewModel x:Key="vm"></viewmodel:MainWindowViewModel>
        <Style TargetType="TextBlock" x:Key="mouseOverStyle">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="FontSize" Value="20"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
            <TextBlock HorizontalAlignment="Center" Name="lblDBAction">aaa</TextBlock>

            <Grid Margin="0 10" HorizontalAlignment="Center">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock Text="Source Server" FontWeight="Bold" Grid.Column="0" HorizontalAlignment="Right" Margin="0 15 10 0"/>
                <ComboBox Grid.Column="1" Margin="0 10 0 0" Height="20" Name="cmbSrcServer" Tag="Server" TabIndex="10"  DisplayMemberPath="ServerName"
                          ItemsSource="{Binding lstServer, Source={StaticResource vm}}"
                          Text="{Binding sv.ServerName, Source={StaticResource vm}, UpdateSourceTrigger=PropertyChanged}"
                          SelectedItem="{Binding sv.ServerName, Source={StaticResource vm}, UpdateSourceTrigger=PropertyChanged}">
                </ComboBox>
            </Grid>

            <Grid Margin="0 10" HorizontalAlignment="Center">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="235"></ColumnDefinition>
                    <ColumnDefinition Width="135"></ColumnDefinition>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Button Grid.Column="1" Content="{Binding SrcFolderCaption, Source={StaticResource vm}}" Name="btnSelFile"  TabIndex="20"
                        Command="{Binding SrcButtonClick, Source={StaticResource vm}}"></Button>
                <TextBlock Text="{Binding sv.SrcFile, Source={StaticResource vm}, UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Margin="20,5,-142,0" Name="lblFileName"
                           Tag="File Name" HorizontalAlignment="Left" Style="{StaticResource mouseOverStyle}"
                           Width="222" FontSize="8">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseEnter">
                            <i:InvokeCommandAction Command="{Binding MouseOverFileName}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </TextBlock>
            </Grid>



服务器型号类:


Server Model class:

    public class Server : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
       private string _SrcFile;

        public string SrcFile
        {
            get
            {
                return _SrcFile;
            }
            set
            {
                _SrcFile = value;
                OnPropertyChanged("SrcFile");
            }
        }

        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

/* =====================================================================

FTPSrcUpdateCommand class

===================================================================== */

    internal class FTPSrcUpdateCommand : ICommand
    {
        Predicate<object> canExecute;
        Action<object> execute;

        public FTPSrcUpdateCommand(Predicate<object> _canexecute, Action<object> _execute)
        {
            canExecute = _canexecute;
            execute = _execute;
        }

        #region ICommand Members
        public bool CanExecute(object parameter)
        {
            return canExecute == null ? true : canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            execute(parameter);
        }
        #endregion
    }
    

MainWindowViewModel.cs

MainWindowViewModel.cs

    public class MainWindowViewModel : ViewModelBase, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        ICommand _commandSrc, _commandDest;
        public Server sv { get; set; }
        MainWindowViewModel vm;
        public MainWindowViewModel()
        {
            SrcFolderCaption = "Select Source File";
            sv = new Server("1123");
            //sv.SrcFile = "";
        }
        private bool CanExecuteSrc(object parameter)
        {
            return true;
        }
        private void ExecuteSrc(object parameter)
        {

//Here the SrcFile property is updated correctly

            sv.SrcFile = sv.SourceFile();
        }
        private bool CanExecuteMouse(object parameter)
        {
            return true;
        }
        private void ExecuteMouse(object parameter)
        {

//Here -- SrcFile property is null.  Function called correctly, I just didn't include that code

            if (sv.SrcFile.Length > 0)
                me.EventAndLabel(me.MouseActionLabel, sv.SrcFile);
        }

        public ICommand SrcButtonClick
        {
            get
            {
                if (_commandSrc == null)
                {
                    _commandSrc = new FTPSrcUpdateCommand(CanExecuteSrc, ExecuteSrc);
                }
                return _commandSrc;
            }
        }

        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

为什么  SrcFile
属性在ExecuteMouse函数中为空? 该属性正确保存在
ExecuteSrc群组。然后我做一个"MouseEnter"
超过存储值的标签(
ExecuteMouse运行
但它现在为空

注意:这是一个更易阅读的答案版本, 


这篇关于mvvm - 属性值不持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆