为什么这个AppendText依赖属性在字符串相等时不打印?

时间:2012-11-29 17:45:47

标签: c# wpf mvvm

如果字符串相同,则不会在WPF表单文本框中打印/绑定/发布到View。例如,如果我使用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));
      }
   }

这是为我打印字节的服务(如果使用随机字符,则工作正常:

   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"

2 个答案:

答案 0 :(得分:1)

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

  public StringBuilder Data
  {
     get { return _data; }
     set
     {
        _data = null;
        OnPropertyChanged("Data");
        _data = value;
        OnPropertyChanged("Data");
     }
  }

答案 1 :(得分: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。