发生的情况是,如果字符串相同,则不是打印/绑定/发布到WPF表单文本框中的视图。例如,如果我使用random来生成字节数组,并将其转换为字符串,那么它确实会post到视图。
下面是视图绑定到的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"。
发布于 2012-11-30 02:18:07
DependencyProperties在触发通知之前比较它们的旧的和新的值,并且只有在确实存在差异时才触发它。解决方案很简单:在设置字符串之前将AppendText设置为null,如下所示
public StringBuilder Data
{
get { return _data; }
set
{
_data = null;
OnPropertyChanged("Data");
_data = value;
OnPropertyChanged("Data");
}
}发布于 2012-12-01 02:58:07
我从我写的一个工作应用程序中找到了这个...也许这对你有帮助。
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 attached属性时调用OnAttach,在取消设置或卸载时调用OnDetach。
https://stackoverflow.com/questions/13631633
复制相似问题