WPF Buttont显示绑定与文本
时间: 2024-05-13 20:19:25 浏览: 16
可以使用以下代码将WPF Button的Text和Visible属性绑定到ViewModel中的属性:
```xaml
<Button Content="{Binding ButtonText}" Visibility="{Binding ButtonVisible}" />
```
其中,`ButtonText`和`ButtonVisible`是ViewModel中的属性。
如果要设置Button的文本内容,可以在ViewModel中设置`ButtonText`属性的值,如:
```csharp
private string _buttonText = "点击我";
public string ButtonText
{
get { return _buttonText; }
set
{
_buttonText = value;
OnPropertyChanged(nameof(ButtonText));
}
}
```
如果要设置Button的可见性,可以在ViewModel中设置`ButtonVisible`属性的值,如:
```csharp
private Visibility _buttonVisible = Visibility.Visible;
public Visibility ButtonVisible
{
get { return _buttonVisible; }
set
{
_buttonVisible = value;
OnPropertyChanged(nameof(ButtonVisible));
}
}
```
其中,`OnPropertyChanged`方法是实现`INotifyPropertyChanged`接口的方法,用于通知WPF界面更新绑定的属性。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)