C# 数据绑定到singleton类
我在这里搜索了很多次,找到了很多例子,但似乎什么都没找到 我已经设置了一个解决方案,其中ViewModel通过定位器引用MainViewModel类。主视图模型类具有:C# 数据绑定到singleton类,c#,wpf,data-binding,C#,Wpf,Data Binding,我在这里搜索了很多次,找到了很多例子,但似乎什么都没找到 我已经设置了一个解决方案,其中ViewModel通过定位器引用MainViewModel类。主视图模型类具有: public NotifyLog Log { get { return LogMgr.Instance.Log; } } 在里面。这允许我具体说明: <TextBox IsEnabled="True" Text="{Binding Log.Text, Mode=OneWay}"
public NotifyLog Log
{
get { return LogMgr.Instance.Log; }
}
在里面。这允许我具体说明:
<TextBox IsEnabled="True" Text="{Binding Log.Text, Mode=OneWay}" />
对于应用程序的初始启动,会填充文本框,但绑定不会自动填充OnPropertyChanged处理程序,因此不会检测到任何更改。我做错了什么,我只是不知道
谢谢你抽出时间,
BlD如果在文本框中键入内容时要更新日志,则需要将绑定模式更改为双向。当您从文本框中退出时,也会触发该事件,而不是在键入的每个字符上 如果要在更改日志时更新文本框,则需要将setter添加到text属性并引发NotifyPropertyChanged事件(在setter中) 还要检查程序的输出是否有一些绑定错误。到行:
<TextBox IsEnabled="True" Text="{Binding Log.Text, Mode=OneWay}" />
尝试添加“UpdatedSourceTrigger”,如下所示:
<TextBox IsEnabled="True" Text="{Binding Log.Text, Mode=OneWay}" />
<TextBox IsEnabled="True" Text="{Binding Log.Text, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />