C# 如何在MVVM Caliburn.Micro中绑定用户控件?
我有一个带有标签的用户控件(下一个UC)。我需要在按钮上单击更改UC标签内容。在UC codebehind上,我创建了DependencyProperty和更改标签的方法C# 如何在MVVM Caliburn.Micro中绑定用户控件?,c#,wpf,mvvm,caliburn.micro,C#,Wpf,Mvvm,Caliburn.micro,我有一个带有标签的用户控件(下一个UC)。我需要在按钮上单击更改UC标签内容。在UC codebehind上,我创建了DependencyProperty和更改标签的方法 public string InfoLabel { get { return (string)this.GetValue(InfoLabelProperty); } set { this.Se
public string InfoLabel
{
get
{
return (string)this.GetValue(InfoLabelProperty);
}
set
{
this.SetValue(InfoLabelProperty, value);
}
}
private static void InfoLabelChangeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UserControl1 uc = d as UserControl1;
uc.CInfoLabel.Content = uc.InfoLabel;
}
public static readonly DependencyProperty InfoLabelProperty = DependencyProperty.Register("InfoLabel", typeof(string), typeof(UserControl1), new PropertyMetadata("", new PropertyChangedCallback(InfoLabelChangeCallback)));
private string infoLabel= "something";
public string InfoLabel1
{
get
{
return infoLabel;
}
set
{
infoLabel = value;
}
}
public void ChangeUserControllButton()
{
InfoLabel1 = "Hello world";
}
在ShellView上,我绑定了控件和按钮
<c:UserControl1 InfoLabel="{Binding InfoLabel1}" />
<Button x:Name="ChangeUserControllButton"/>
问题是当UC初始化时,它就工作了。我的意思是,UC的标签将包含内容“某物”,但当我点击按钮时,内容不会更改为“Hello world”。如何使其正确?视图模型需要实现
INotifyPropertyChanged
,以便能够通知UI它应该刷新/更新,因为绑定模型已更改。我相信已经有一个基类提供了这种功能
参考文献
更新从PropertyChangedBase
派生的ShellViewModel
,然后在属性中调用一个可用方法,使视图模型能够通知UI属性已更改
public class ShellViewModel : PropertyChangedBase {
private string infoLabel= "something";
public string InfoLabel1 {
get {
return infoLabel;
}
set {
infoLabel = value;
NotifyOfPropertyChange();
//Or
//Set(ref infoLabel, value);
}
}
public void ChangeUserControllButton() {
InfoLabel1 = "Hello world";
}
}
阅读更多有关如何使用该框架的示例。视图模型需要实现
INotifyPropertyChanged
,以便能够通知UI由于绑定模型已更改,它应该刷新/更新。我相信已经有一个基类提供了这种功能
参考文献
更新从PropertyChangedBase
派生的ShellViewModel
,然后在属性中调用一个可用方法,使视图模型能够通知UI属性已更改
public class ShellViewModel : PropertyChangedBase {
private string infoLabel= "something";
public string InfoLabel1 {
get {
return infoLabel;
}
set {
infoLabel = value;
NotifyOfPropertyChange();
//Or
//Set(ref infoLabel, value);
}
}
public void ChangeUserControllButton() {
InfoLabel1 = "Hello world";
}
}
阅读更多关于如何使用该框架的示例