我有一个ListBox
绑定到ObservableCollection
。每当我按下列表框项目时,我都可以在我设计的右侧面板上看到信息。我这样做,通过Binding
所选项目到每个TextBox
这样的:如何保存数据后按钮点击绑定列表框
<TextBox Name="TextBoxEditName" Text="{Binding ElementName=ListBoxClients, Path=SelectedItem.Name}" />
哪里ListBoxClients
是包含ObservableCollection
数据我ListBox
对象,并在这一特定TextBox
我展示它的Name
。
注:该部分工作正常。如果我在列表中选择另一个项目,它会更改。
现在来了棘手的部分: 每当我编辑TextBox
,ListBox
项目正在同时更新。当我按下Save button
而不是前面时,我想要ListBox
项目更改。
我尝试了所有Binding Modes
:
- 单向
- OneWayToSource
- ....
但同样的效果发生了:当我TextBox
失去焦点时,ListBox
项目被改变。所以... 当我按下按钮而不是之前,我该如何激发保存事件?
我后面的代码:
private void ButtonSaveChanges_OnClick(object sender, RoutedEventArgs e)
{
DtoCustomer selectedCustomer = (DtoCustomer) ListBoxClients.SelectedItem;
if (selectedCustomer == null) return;
BindingExpression b = BindingOperations.GetBindingExpression(ListBoxClients, ListBox.ItemsSourceProperty);
b.UpdateSource();
}
它不工作...什么是错误的,我后面的代码?我的ListBox
绑定到ObservableCollection
。
如果我理解更新,添加UpdateSourceTrigger =显式绑定,然后调用UpdateSource方法更新源代码 http://msdn.microsoft.com/fr-fr/library/syste m.windows.data.binding.updatesourcetrigger(v = vs.110).aspx – mlemay
是的,就是这样。但我的BindingExpression不起作用。只是看什么可能是错的。 – Sonhja