【问题标题】:hot to notify viewmodel property form model so that property value reflect in view热通知 viewmodel 属性表单模型,以便属性值反映在视图中
【发布时间】:2015-08-26 21:15:45
【问题描述】:
public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {   
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

这是实现 INotifyPropertyChanged 的​​基类

public class LogActivity : ViewModelBase
{
    private string messageLog;

    public string MessageLog
    {
        get
        {
            return messageLog;
        }
        set
        {
            if (value != messageLog)
            {
                messageLog = value;
                NotifyPropertyChanged("MessageLog");
            }
        }
    }  

}

这是我的视图模型类

public class SingleMessageViewModel : ViewModelBase
{
 private LogActivity messagelog;

public SingleMessageViewModel()
    {

        messagelog = new LogActivity();
}   


public LogActivity MessageLog
    {
        get
        {
            return messagelog;
        }
        set
        {
            if (value != messagelog)
            {
                messagelog = value;
                NotifyPropertyChanged("MessageLog");
            }
        }
    }  
 }

这是我对以上属性绑定的看法:

<TextBox x:Name="TxtLog" Text="{Binding LogMessage, UpdateSourceTrigger=PropertyChanged}"  Grid.Row="1" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" />

我想在我的一个模型类中使用此属性,并将有关属性更改的通知发送到 viewmodel,以便它可以绑定到上面给出的视图。

请大家为此提供一些帮助。在此先感谢.. :)

【问题讨论】:

  • 质量差的问题,充满了错别字。如果我有足够的声誉,我会投反对票。

标签: c# wpf mvvm


【解决方案1】:

此代码:

<TextBox x:Name="TxtLog" Text="{Binding LogMessage, UpdateSourceTrigger=PropertyChanged}"  Grid.Row="1" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" />

需要:

<TextBox x:Name="TxtLog" Text="{Binding MessageLog.MessageLog, UpdateSourceTrigger=PropertyChanged}"  Grid.Row="1" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" />

注意 Binding,您正在绑定到 LogActivity 中的一个属性,它需要正确地指向它。

【讨论】:

  • 作为旁注,可以尝试将第一类重命名为您绑定到的字符串以外的其他名称...看起来/感觉很奇怪。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多