- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在我的 Silverlight 项目中,我正在创建文本框,这些文本框在运行时双向数据绑定(bind)到某个上下文。一个方向(从源到目标)的绑定(bind)似乎工作正常,但另一个方向(从目标回到源)没有显示任何效果.
这是数据上下文:
public class Leg : INotifyPropertyChanged {
private string passengers;
public string Passengers {
get { return passengers; }
set {
// here I have a breakpoint.
passengers = value;
FirePropertyChanged("Passengers");
}
}
private void FirePropertyChanged (string property) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
然后在另一个地方我正在创建一个新的 TextBox 控件以及它的绑定(bind):
Binding passengersBinding = new Binding();
// viewModelLeg is an instance of the class Leg from above
passengersBinding.Source = viewModelLeg;
passengersBinding.Path = new PropertyPath("Passengers");
passengersBinding.Mode = BindingMode.TwoWay;
legItem.paxTextBox.SetBinding(TextBox.TextProperty, passengersBinding);
现在,当我更改 Passengers 字符串的值时,绑定(bind)到它的相应文本框会正确更新其文本。 所以一切都很好。
但是当我手动更改文本框的文本然后使文本框失去焦点时,没有任何反应 - 即没有发生双向绑定(bind) - 文本框的新文本值没有向下传播到来源!
我在 passengers-attribute 的 setter 处设置了一个断点(用上面的断点注释标记)。 当我得到所有这些权利时,当绑定(bind)的目标值已更改以更新源时,绑定(bind)引擎也会使用此公共(public) setter - 所以当发生这种情况时,必须命中断点。 但他没有!所以看来我可以用我的文本框做我想做的事(玩焦点或按回车)它永远不会更新它的源。
我在监督什么吗?在我的代码或我的想法中一定有一个严重的错误..我真的很感激任何想法......
编辑:
在下文中,我将尝试演示如何创建我的 XAML 对象和我的 DataContext 对象。因为我在运行时创建 XAML 控件及其绑定(bind),所以我还没有找到很好的解决方案来很好地实现 MVVM 方法。所以我正在做以下事情(这可能不是最好的方法):
我建模的情况是我有一个 UserControl(称为 LegItem),它(主要)由文本框组成。在运行时,用户可以根据自己的意愿创建尽可能多的用户控件(一个接一个)。
在我的 ViewModel 端,我有一个类(称为 Leg)作为一个 LegItem 的 ViewModel。所以当我说 n (XAML-) LegItems 时,我也有 n Leg 实例。我将这些 Leg 对象存储在一个列表中。
因此,每当用户点击“添加一条新腿”按钮时,我都会执行以下操作:
// here we are inside the applications view in an .xaml.cs file
public void AddLeg () {
// this is going to serve as the ViewModel for the new LegItem
// I am about to create.
Leg leg = viewModel.insertLeg();
// here I am starting to create the visual LegItem. The ViewModel object
// I have created in the previous step is getting along with.
createLegItem(leg);
}
// the primary job here is to bind each contained textbox to its DataContext.
private LegItem createLeg (Leg viewModelLeg) {
// create the visual leg item control element
// which is defined as a XAML UserControl.
LegItem legItem = new LegItem();
Binding passengersBinding = new Binding();
// viewModelLeg is an instance of the class Leg from above
passengersBinding.Source = viewModelLeg;
passengersBinding.Path = new PropertyPath("Passengers");
passengersBinding.Mode = BindingMode.TwoWay;
legItem.paxTextBox.SetBinding(TextBox.TextProperty, passengersBinding);
}
// on the viewModel side there is this simple method that creates one Leg object
// for each LegItem the View is creating and stores inside a simple list.
public Leg InsertLeg () {
Leg leg = new Leg();
legList.add(leg)
return leg;
}
最佳答案
新答案
由于您提到您的绑定(bind)实际上是自定义 UserControl
而不是 TextBox
,我建议查看您的 UserControl
的 XAML > 并确保它正确绑定(bind)数据
旧答案
我对新的 Silverlight 项目进行了快速测试,发现启动项目是 SilverlightApplication1.Web
,而不是 SilverlightApplication
。
这意味着当我运行项目时,setter 中的断点实际上不会被命中。你会注意到断点圆只是轮廓,没有填充颜色。如果将鼠标悬停在它上面,它会显示
The breakpoint will not currently be hit. No symbols have been loaded for this document
如果我启动 SilverlightApplication1
而不是 .Web
版本,断点就会命中。
无论我启动哪个版本,属性都是正确更改,但是如果我使用.Web
版本启动项目,断点不会被击中。我怀疑这是你的问题。
关于c# - 双向绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928996/
我有 2 个类:User 和 UserPicture,它们具有 1:1 关系。 public class User { @Id @GeneratedValue(strategy=G
使用ssh转发时,我无法针对远程服务器使用cvs和ftp进行提交。是否可以让服务器对我的机器发起请求-我希望服务器上的Web应用程序调用我的机器上的REST方法。 谢谢。 尼古拉·G。 最佳答案 是的
我正在 Python 2.7.12 中实现双向 A* 算法,并在 Russell 和 Norvig 第 3 章的罗马尼亚 map 上进行测试。边具有权重,目的是找到两个节点之间的最短路径。 这是测试图
您能否建议一种映射或类似的数据结构,让我们可以轻松地相互获取值和键。也就是说,每个都可以用来寻找另一个。 最佳答案 Java 在其标准库中没有双向映射。 例如使用 BiMap 来自Google Gua
我想同步两个数据库运行时 服务器 A:安装了公共(public) IP 和 mysql 的 Amazon ec2。服务器B:这是局域网中带有mysql的私有(private)机器。 (IP是私有(pr
保存双向@OneToOne 映射时,hibernate 是否应该在两个表上都记录? 我有一个包含 applicant_id 列的表 interview,它引用了包含字段 interview_id 的
我喜欢新的 SwipeRefreshLayout!它看起来很棒,而且非常容易使用。但我想在两个方向上使用它。我有一个消息屏幕,我想通过从上到下滑动来加载旧消息,我想通过从下到上滑动来加载新消息。 这个
使用 ICS 4.0.1(愿意升级到 4.0.3)(不会 root 和重写 android 操作系统) 在接收到 android beam 后,是否可以将 NDEF 消息发送回 android 手机
我想知道处理这种 git 场景的最佳方法: Git 仓库:CoreProduct Git repo b: SpecificCustomerProduct 是从 a fork 出来的 到目前为止,我们一
这个问题在这里已经有了答案: How to implement an efficient bidirectional hash table? (8 个回答) 关闭2年前。 我在 python 中做这个
您能否推荐一种 map 或类似的数据结构,我们可以在其中轻松地从彼此获取值和键。也就是说,每个都可以用来寻找另一个。 最佳答案 Java 在其标准库中没有双向映射。 例如使用 BiMap 来自 Goo
Java中是否有类似双面列表的东西?也许第三方实现? 这里有一个小例子来证明我的想法。 原始状态: 答:0-1-2-3 | | | | 乙:0-1-2-3 删除 B 中的元素 1 后: 空值 | 答:
我有两个实体通过这样的双向 OneToOne 关联连接: @Entity class Parent { @NotNull String businessKey; @OneToO
我已将 Vagrant 配置为使用 Rsync 共享文件夹而不是(非常慢)vboxsf VirtualBox 默认提供的文件系统: Vagrant.configure("2") do |config|
@keyframes mgm { from { max-height: 250px; } to { max-height: 0px; } } .mgm {
我想了解有关使用双向 LSTM 进行序列分类时合并模式的更多详细信息,尤其是对于我还不清楚的“Concat”合并模式。 根据我对这个方案的理解: 在将前向和后向层的合并结果传递到 sigmoid 函数
我有兴趣将本地 git 存储库设置为远程存储库的镜像。我已经阅读了一些可能相关的帖子,但主要区别在于我需要对两个存储库进行读写访问。 大多数时候,用户会针对 Repo A 工作,但是有时他们会针对 R
我已经仔细阅读了文档 https://firebase.google.com/docs/database/web/read-and-write以及网上很多例子。但这里有一个脱节:在将对象添加到数据库时
这个问题已经有答案了: Hibernate bidirectional @ManyToOne, updating the not owning side not working (3 个回答) 已关闭
我知道有很多关于它的问题,但我找不到针对我的问题的好的答案。 我使用 Jboss 作为 7,Spring 和 Hibernate (4) 作为 JPA 2.0 提供程序,因此我有简单的 @OneToM
我是一名优秀的程序员,十分优秀!