2009-08-23 42 views
1

我有一个ListBox,它的每个项目都有一个按钮,我设置了Binding.UpdateSourceTrigger显式的数据项中的所有文本框。如何使用UpdateSourceTrigger =在像ListBox等Items控件中显式

我添加了一个处理程序到按钮的点击,现在是什么?

如何从控件收集信息?他们没有密钥他们是动态的,我如何获得他们的BindingExpressions?

<ListBox ItemsSource="{Binding Path=Phones}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate DataType="{x:Type data:Phone}"> 
      <StackPanel Style="{StaticResource StackPanelStyle}">     
       <TextBox Margin="5" VerticalAlignment="Center" Name="tbNumber" 
Text="{Binding Number, ValidatesOnExceptions=True, UpdateSourceTrigger=Explicit}" 
/> 
       <Button Click="btnSavePhone_Click" Margin="5" 
Content="_Update" IsEnabled="{Binding IsValid}" />     
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

你可以添加一些你如何设置的代码示例吗? – 2009-08-23 04:38:36

回答

1
Private Sub btnSavePhone_Click(sender As Button, e As RoutedEventArgs) 
    'As I only have one TextBox I can use the following filter, 
    'you can of corse change it to Where c.Name = "tbNumber" 
    Dim tbNumber = From c As FrameworkElement In _ 
     DirectCast(sender.Parent, StackPanel).Children Where TypeOf c Is TextBox 
    Dim x = tbNumber.ToList 
    Dim be = tbNumber.Cast(Of TextBox).First _ 
       .GetBindingExpression(TextBox.TextProperty) 

    If Not be.HasError Then be.UpdateSource() 
End Sub 

更新
在某些情况下,BindingGroup将是最好的解决办法,则U调用BindingGroup.UpdateSources

+0

感谢有关BindingGroup的更新。帮助很多! – Scott 2010-05-27 15:26:46

相关问题