2009-08-23 4 views
1

J'ai un ListBox que chacun de ses éléments a un bouton, je mets toutes les zones de texte dans le dataitem que Binding.UpdateSourceTrigger est explicite.Comment utiliser UpdateSourceTrigger = Explicite dans un contrôle Items comme ListBox etc

J'ai ajouté un gestionnaire au clic du bouton, et maintenant?

Comment puis-je collecter les informations à partir des contrôles? ils n'ont pas de clé, ils sont dynamiques, comment puis-je obtenir leur 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

Pouvez-vous ajouter un exemple de code de la façon dont vous avez configuré ceci? –

Répondre

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 

Mise à jour
Dans certains scénarios, BindingGroup serait la meilleure solution, puis appelez BindingGroup.UpdateSources U.

+0

Merci pour la mise à jour sur BindingGroup. Ça aide beaucoup! – Scott