C# 如何从助手类更新WPF绑定
我相信这是一个非常基本的问题,但我甚至不知道谷歌和自学的技术术语/行话 我创建了一个实现INotifyPropertyChanged的简单模型C# 如何从助手类更新WPF绑定,c#,wpf,data-binding,C#,Wpf,Data Binding,我相信这是一个非常基本的问题,但我甚至不知道谷歌和自学的技术术语/行话 我创建了一个实现INotifyPropertyChanged的简单模型 public class PushNotes : INotifyPropertyChanged { public string CompletePushNotes { get; set; } } cs中的绑定: evt_pushNotes = new PushNotes() { Com
public class PushNotes : INotifyPropertyChanged
{
public string CompletePushNotes { get; set; }
}
cs中的绑定:
evt_pushNotes = new PushNotes()
{
CompletePushNotes = "HelloThere"
};
this.DataContext = evt_pushNotes;
//snip later in code
Helpers.UpdateCompletePushNotes();
在XAML中:
<xctk:RichTextBox x:Name="PushEmail" Text="{Binding Path=CompletePushNotes, Mode=OneWay}" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="40,398,40,40">
<xctk:RichTextBox.TextFormatter>
<xctk:PlainTextFormatter />
</xctk:RichTextBox.TextFormatter>
</xctk:RichTextBox>
现在一切都好了,但是我在助手类中有一个方法需要更改CompletePushNotes
我知道这是一个过于简单的问题,但我不知道我需要学习什么
所以我要让我的PushNotes类是静态的,还是单例的。是否有一些全局绑定“树”我可以走着找到我的实例化和绑定的PushNotes类,该类附加到UI元素
不是在找讲义,只是需要知道我在找什么
TIA您的PushNotes类未实现INotifyPropertyChanged接口。一旦您实现了它,您需要修改CompletePushNotes属性以具有一个支持字段,并且在属性的setter中,您可以引发PropertyChanged事件以通知UI源属性更新
public class PushNotes : INotifyPropertyChanged
{
string completePushNotes;
public string CompletePushNotes
{
get
{
return completePushNotes;
}
set
{
completePushNotes = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
将PushNotes类设置为静态对您没有帮助。您似乎有一个PushNotes实例(evt_PushNotes)的某种变量,因此只需执行以下操作:
evt_pushNotes.CompletePushNotes = something;
如果您有一个执行某些操作的helper类,请在helper类中调用该方法并获取值,或者将PushNotes实例作为参数传递到helper类中
internal static class Helpers
{
internal static void UpdateCompletePushNotes(PushNotes pushNotes)
{
pushNotes.CompletePushNotes = something;
}
}
好酷…我已经添加了…我的Helper类是如何得到这个特定的实例化的?我看不出你是如何/在哪里使用Helper类的。啊,是的,我是从理论上讲的,因为我不想让人们用勺子喂我。我将更新OP>@GPGVM:您打算如何调用Helper方法?你不能传入你计划更新的实例吗?什么叫UpdateCompletePushNotes方法?
internal static class Helpers
{
internal static void UpdateCompletePushNotes(PushNotes pushNotes)
{
pushNotes.CompletePushNotes = something;
}
}