【问题标题】:MVVMLight C# how to change a button contentMVVMLight C# 如何更改按钮内容
【发布时间】:2020-03-08 19:15:28
【问题描述】:

我是使用 MVVMLight 的 WPF 新手,并且很难掌握事情的工作原理。 我在 xaml 中有一个按钮:

<Button x:Name="button" Content="Button" 
    HorizontalAlignment="Left" 
    Margin="29,374,0,0" 
    VerticalAlignment="Top" Width="75" 
    Command="{Binding BeginCollectionCommand}"/>

并让视图模型响应按钮按下。 BeginCollectionCommand = new RelayCommand(BeginCollectionCommandExecute, () =&gt; true);

我在

上找不到我的问题的答案
  • 如何将按钮设置为禁用
  • 如何将“content=”设置为“working...”
  • 项目完成后如何重新启用按钮
  • 如何将“content=”设置为“Done”
  • 我还想等待 5 秒再将内容设置为“开始”。我相信我可以用 thread.sleep(5000) 做到这一点,但其他部分我不清楚。

View Model 代码将按钮绑定“BeginCollectionCommand”定义为

public RelayCommand BeginCollectionCommand { get; set; }
    public MainWindowViewModel()
  {
    BeginCollectionCommand = new RelayCommand(BeginCollectionCommandExecute, () => true);
    //at this point i believe is where i set the button content to "working..."
    //and disable.
  }

  public void BeginCollectionCommandExecute()
  {
    /// do my working class code

    //I think at this point I want to set the code to change button content to
    //enable, conent to "done" then wait and set to "start"
  }

有人可以帮忙吗?

【问题讨论】:

    标签: c# wpf mvvm-light


    【解决方案1】:

    你的问题可以总结为三种问题。

    1. 如何启用或禁用按钮。
    2. 如何更改按钮的内容。
    3. 如何在一段时间后更改内容。

    对于第一个和第二个问题,将您的按钮 IsEnable 绑定到 viewModel 中的属性并将内容绑定到字符串

    在 xaml 中。

    <Button x:Name="button" 
        Content="{Binding ButtonString}"
        HorizontalAlignment="Left" 
        Margin="29,374,0,0" 
        VerticalAlignment="Top" Width="75"
        IsEnabled="{Binding ButtonEnabled}"
        Command="{Binding BeginCollectionCommand}"/>
    

    在视图模型中

        // Set true or button cannot be pressed.
        bool m_Enabled = true;
        public bool ButtonEnabled
        {
            get{  return m_Enalbed; }
            set{ m_Enabled = value; 
             // RaisePropertyChanged MUST fire the same case-sensitive name of property
                 RaisePropertyChanged( "ButtonEnabled" );
               }
            }
        }
    
        public bool ButtonString
        {
         get;set;
        }
        bool m_String = false;
        public bool ButtonString
        {
            get{  return m_String; }
            set{ m_String = value; 
                 // RaisePropertyChanged MUST fire the same case-sensitive name of property
                 RaisePropertyChanged( "ButtonString" );
               }
            }
        }
    
    
    public void BeginCollectionCommandExecute()
    {
        //I simplify the way of variable passing, 
        //You need to take care of how to set property from command to viewmodel. 
        //A method delegate would be okay.
        ButtonEnabled = false;
        ButtonString = "Working";
        // working here
        ButtonEnabled = true;
        ButtonString = "Done";
    }
    

    对于第三个问题,你可以使用定时器或者ThreadSleep也可以。

    【讨论】:

    • 对您的示例代码进行了更正,否则它非常有帮助。正是我需要的。
    • bool m_Enabled = true; //如果不是真的按钮永远不可用 public bool ButtonEnabled { get { return m_Enabled; } 设置 { m_Enabled = 值; RaisePropertyChanged("BtnEnabled"); } } 字符串 m_String = "开始";公共字符串 ButtonString { 获取 { 返回 m_String; } 设置 { m_String = 值; RaisePropertyChanged("ButtonString"); } }
    • 我注意到在运行时我没有看到按钮内容更改和启用禁用的状态更改。我的工薪班有两分钟,所以我知道有足够的时间进行改变。有什么建议吗?
    • 您的意思是状态更改最多需要两分钟吗?尝试通过 Thread.Sleep 模拟您的操作并检查它是否正常工作。
    • 如果它不起作用,我想出了两个可能的原因。 1.RaisePropertyChanged的参数区分大小写。 2. 测试时将ButtonStringButtonEnabled 放在viewmodel 中,不要放在嵌套属性中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多