C# 如何根据按钮点击设置标签内容
我的viewmodel类中有一个方法,通过单击按钮调用该方法并执行一些操作。现在,我的xaml文件中有一个标签和按钮:C# 如何根据按钮点击设置标签内容,c#,wpf,button,mvvm,label,C#,Wpf,Button,Mvvm,Label,我的viewmodel类中有一个方法,通过单击按钮调用该方法并执行一些操作。现在,我的xaml文件中有一个标签和按钮: <Label Content="" Height="20" HorizontalAlignment="Center" Margin="0,50,0,0" Name="label1" VerticalAlignment="Top" Width="119" /> <Button Content="Sync" Height="23" Command="{Bindin
<Label Content="" Height="20" HorizontalAlignment="Center" Margin="0,50,0,0" Name="label1" VerticalAlignment="Top" Width="119" />
<Button Content="Sync" Height="23" Command="{Binding Path=SyncCommand}" HorizontalAlignment="Center" Margin="0,15,0,0" Name="button1" VerticalAlignment="Top" Width="100" />
因此,基本上在这两个地方(strBadResp)的结果都应该在标签中。我希望我已经讲清楚了。我是WPF世界的新手。请帮忙 您必须在代码隐藏中创建标签对象,如下所示
var lableMSG = new Lable();
lableMSG.Content = "Message string";
希望这对你有帮助 将标签上的内容属性绑定到视图模型上的属性。要更新标签时,请更新响应属性 查看
<Label Content="{Binding Response}" Height="20" HorizontalAlignment="Center" Margin="0,50,0,0" Name="label1" VerticalAlignment="Top" Width="119" />
我遵循MVVM模式。你的答案不是我所期望的:(在添加上面的代码之后,我刚刚添加了Response=strBadResp;我得到了:)谢谢。
var lableMSG = new Lable();
lableMSG.Content = "Message string";
<Label Content="{Binding Response}" Height="20" HorizontalAlignment="Center" Margin="0,50,0,0" Name="label1" VerticalAlignment="Top" Width="119" />
public class YourViewModel : INotifyPropertyChanged {
string response;
public string Response {
get { return this.response; }
set {
if (this.response == value)
return;
this.response = value;
NotifyPropertyChanged("Response");
}
}
public event NotityPropertyChangedEventHandler = delegate {}
void NotifyPropertyChanged(string propertyName) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName);
}
}