c# - MVVMLight C# 如何更改按钮内容
问题描述
我是使用 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, () => true);
我未能找到我的问题的答案
- 如何设置按钮禁用
- 如何将“content=”设置为“working...”
- 项目完成后如何重新启用按钮
- 如何将“内容=”设置为“完成”
- 我还想等 5 秒再将内容设置为“开始”。我相信我可以用 thread.sleep(5000) 做到这一点,但其他部分我不清楚。
视图模型代码将按钮绑定“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"
}
有人可以帮忙吗?
解决方案
你的问题可以总结为三种问题。
- 如何启用或禁用按钮。
- 如何更改按钮的内容。
- 如何在一段时间后更改内容。
对于第一个和第二个问题,将您的按钮绑定IsEnable
到 viewModel 中的属性并将内容绑定到字符串
在 xml 中。
<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 即可。
推荐阅读
- arrays - 如何遍历字典内的多个嵌套列表?
- python - 怪异的Python堆:heappop()第一次输出
- r - R导入音频文件以计算音频多样性时出错
- c++ - 可变参数模板构造函数填充 std::variant 的内部向量
- sql - 如何在 Hive 或 Presto 中将以下字典格式列转换为不同的格式?
- php - 如何在 Laravel/Eloquent 中查询嵌套的不同记录?
- reactjs - 为什么 useEffect 的依赖数组中需要历史记录
- r - 需要帮助制作 Ordihull
- javascript - 单击按钮时在 javascript 中执行 selenium 脚本
- reactjs - 电子商务应用 OrderScreen 处理的最佳实践?