2

发生的情况是,如果字符串相同,它不会在 WPF 表单文本框中打印/绑定/发布到视图。例如,如果我使用 random 来生成我正在制作成字符串的字节数组,那么它确实会发布到视图中。

这是视图绑定到的我的 ViewModel:

   public class ViewModel : INotifyPropertyChanged
   {
      public StringBuilder Data
      {
         get { return _data; }
         set
         {
            _data = value;
            OnPropertyChanged("Data");
         }
      }

      private Service service = new Service();
      private StringBuilder _data;

      public ViewModel()
      {
         service.BytesArrived += ServiceOnBytesArrived;
         ThreadPool.QueueUserWorkItem(starupService);
      }

      private void starupService(object state)
      {
         service.Start();
      }

      private void ServiceOnBytesArrived(byte[] bytes)
      {
         var sBuilder = new StringBuilder();
         foreach (var b in bytes)
         {
            sBuilder.Append(b.ToString() + ", ");
         }

         Data = sBuilder;
      }

      public event PropertyChangedEventHandler PropertyChanged;

      protected virtual void OnPropertyChanged(string propertyName)
      {
         var handler = PropertyChanged;
         if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
      }
   }

这是只为我打印字节的服务(如果使用 random 则可以正常工作:

   public class Service
   {
      public void Start()
      {
         var random = new Random(DateTime.Now.Minute);

       while (true)
       {
        //random.NextBytes(bytes);
        for (int i = 0; i < 10; i++)
        {
           bytes[i] = 0;
           Thread.Sleep(10);
        }
        //Thread.Sleep(100);
        BytesArrived(bytes);
       }
    }

  private byte[] bytes = new byte[10];
  public event Action<byte[]> BytesArrived;
}

这是我正在使用的使用 AppendText 的依赖属性:

   public static class TextBoxAttachedBehaviors
   {
      #region AppendText Attached Property

      public static string GetAppendText(TextBox textBox)
      {
         return (string)textBox.GetValue(AppendTextProperty);
      }

      public static void SetAppendText(
         TextBox textBox, string value)
      {
         textBox.SetValue(AppendTextProperty, value);
      }

      public static readonly DependencyProperty AppendTextProperty =
         DependencyProperty.RegisterAttached(
            "AppendText",
            typeof(string),
            typeof(TextBoxAttachedBehaviors),
            new UIPropertyMetadata(null, OnAppendTextChanged));

      private static void OnAppendTextChanged(DependencyObject d,
                                              DependencyPropertyChangedEventArgs e)
      {
         if (e.NewValue == null)
            return;
         TextBox textBox = d as TextBox;
         textBox.AppendText(e.NewValue.ToString());
      }

      #endregion
   }

XAML:

<TextBox attachedBehaviors:TextBoxAttachedBehaviors.AppendText="{Binding TextBoxAppend}"/>

如果你有 ReSharper,它会提供替换命名空间,例如attachedBehaviors:用实际附加行为的类的链接,在我的例子中是xmlns:attachedBehaviors="clr-namespace:Support.NetworkMonitor.AttachedBehaviors".

4

2 回答 2

1

DependencyProperties 在触发通知之前比较它们的旧值和新值,并且仅在确实存在差异时才触发它。解决方案很简单:在设置字符串之前将 AppendText 设置为 null,例如

  public StringBuilder Data
  {
     get { return _data; }
     set
     {
        _data = null;
        OnPropertyChanged("Data");
        _data = value;
        OnPropertyChanged("Data");
     }
  }
于 2012-11-29T18:18:06.710 回答
0

我从我写的一个工作应用程序中找到了这个......也许这有帮助。

    Public Class TextBoxLog
    Inherits Freezable
    Implements WPFGlue.Framework.IStickyComponent

    Private _AppendTextDelegate As Action(Of String)
    Private _ScrollToEndDelegate As Action
    Private _ResetDelegate As Action

    Public Shared ReadOnly LogProperty As DependencyProperty = DependencyProperty.RegisterAttached("Log", GetType(TextBoxLog), GetType(TextBoxLog), New PropertyMetadata(AddressOf WPFGlue.Framework.StickyComponentManager.OnStickyComponentChanged))
    Public Shared Function GetLog(ByVal d As DependencyObject) As TextBoxLog
        Return d.GetValue(LogProperty)
    End Function
    Public Shared Sub SetLog(ByVal d As DependencyObject, ByVal value As TextBoxLog)
        d.SetValue(LogProperty, value)
    End Sub


    Public Shared ReadOnly LogMessageProperty As DependencyProperty = DependencyProperty.Register("LogMessage", GetType(String), GetType(TextBoxLog), New PropertyMetadata(AddressOf OnLogMessageChanged))
    Public Property LogMessage As String
        Get
            Return GetValue(LogMessageProperty)
        End Get
        Set(ByVal value As String)
            SetValue(LogMessageProperty, value)
        End Set
    End Property
    Private Shared Sub OnLogMessageChanged(ByVal d As TextBoxLog, ByVal e As DependencyPropertyChangedEventArgs)
        If e.NewValue IsNot Nothing Then
            d.WriteLine(e.NewValue)
        End If
    End Sub

    Protected Overridable Sub Attach(base As Object)
        If Not TypeOf base Is System.Windows.Controls.Primitives.TextBoxBase Then
            Throw New ArgumentException("Can only be attached to elements of type TextBoxBase")
        End If
        Dim tb As System.Windows.Controls.Primitives.TextBoxBase = base
        _AppendTextDelegate = AddressOf tb.AppendText
        _ScrollToEndDelegate = AddressOf tb.ScrollToEnd
        _ResetDelegate = AddressOf Me.Reset
    End Sub

    Protected Overridable Sub Detach(ByVal base As Object)
        _AppendTextDelegate = Nothing
        _ScrollToEndDelegate = Nothing
        _ResetDelegate = Nothing
    End Sub

    Private Sub Reset()
        SetCurrentValue(LogMessageProperty, Nothing)
    End Sub

    Protected Overrides Function CreateInstanceCore() As System.Windows.Freezable
        Return New TextBoxLog
    End Function

    Public Overridable Sub Write(message As String)
        If _AppendTextDelegate IsNot Nothing Then
            _AppendTextDelegate.Invoke(message)
            _ScrollToEndDelegate.Invoke()
            '               Me.Dispatcher.Invoke(_ResetDelegate, Windows.Threading.DispatcherPriority.Background)
        End If
    End Sub

    Public Overridable Sub WriteLine(message As String)
        If _AppendTextDelegate IsNot Nothing Then
            _AppendTextDelegate.Invoke(message)
            _AppendTextDelegate.Invoke(vbNewLine)
            _ScrollToEndDelegate.Invoke()
            '                Me.Dispatcher.Invoke(_ResetDelegate, Windows.Threading.DispatcherPriority.Background)
        End If
    End Sub

    Public ReadOnly Property Mode As Framework.AttachMode Implements Framework.IStickyComponent.Mode
        Get
            Return Framework.AttachMode.Immediate
        End Get
    End Property

    Public Sub OnAttach(base As Object, e As System.EventArgs) Implements Framework.IStickyComponent.OnAttach
        If e Is System.EventArgs.Empty Then
            Attach(base)
        End If
    End Sub

    Public Sub OnDetach(base As Object, e As System.EventArgs) Implements Framework.IStickyComponent.OnDetach
        If e Is System.EventArgs.Empty Then
            Detach(base)
        End If
    End Sub
End Class

出于本文的目的,您可以假设在设置 Log 附加属性时调用 OnAttach,在未设置或卸载时调用 OnDetach。

于 2012-11-30T18:58:06.813 回答