简体   繁体   English

我想了解有关WPF绑定文本框的内容

[英]I want to know about WPF binding Textbox line down

I am trying to make a logger system with WPF in xaml. 我正在尝试使用xaml中的WPF创建一个记录器系统。 I made the below biding code: 我做了下面的出价代码:

<TextBlock x:Name="textBlock"
           HorizontalAlignment="Left"
           Margin="10,194,0,0"
           TextWrapping="Wrap"
           VerticalAlignment="Top"
           Height="51"
           Width="366"
           Text="{Binding LogView,
                          Source={StaticResource logViewModel},
                          UpdateSourceTrigger=PropertyChanged}" />

The Binding text is working correctly, but in the textbox, string is updated only on the first line in the textbox. 绑定文本正常工作,但是在文本框中,仅在文本框中的第一行更新了字符串。 The string should be updated on the next line, and I can't use append string like the below code because I will be parsing Character(ex. D,E,I) and then changing the string color: 该字符串应在下一行更新,并且我不能像下面的代码那样使用附加字符串,因为我将解析Character(ex。D,E,I),然后更改字符串颜色:

mLogViewStr+=value;

help me please... 请帮帮我...

logViewModel : logViewModel:

 namespace StretcherUI.Device
    {
        class LogViewModel : ILogHandler,
                             INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            static string savePath = @"d:\log.txt";
            const int logListSize = 500;
            public LogViewModel()
            {
                Logger.sLogHandler = this;
            }


            private string mLogViewStr;
            public string LogView
            {
                get { return mLogViewStr; }
                set
                {
                    mLogViewStr = value;               
                }
            }

            protected void OnPropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(
                                    this,
                                    new PropertyChangedEventArgs(propertyName));
            }

            public void onLog(string logFmt, params object[] arg)
            {
                LogView = string.Format(logFmt, arg);
            }
        }
    }

you should call OnPropertyChanged in your public setter, that way WPF view can understand that it has pending update 您应该在公共设置器中调用OnPropertyChanged ,这样WPF视图可以了解它具有待处理的更新

  public string LogView
            {
                get { return mLogViewStr; }
                set
                {
                    mLogViewStr = value;
                    OnPropertyChanged("LogView");
                }
            }

Binding from model to view work only if you notify View. 仅当您通知View时,才将模型绑定到View工作。 So in order to notify view, model should implement INotifyPropertyChanged (which you did) and call PropertyChanged every time bound property changed (which you missed). 因此,为了通知视图,模型应该实现INotifyPropertyChanged(您所做的)并在每次绑定属性更改(您错过了)时调用PropertyChanged。 Just modify your code like this: 只需像这样修改您的代码:

public string LogView {
  get {
    return mLogViewStr;
  }
  set {
    mLogViewStr = value;
    OnPropertyChanged(nameof(LogView))             
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM