views:

523

answers:

1

Hi!

I have a ListBox that each of its items has a button, I set all the textboxes in the dataitem that the Binding.UpdateSourceTrigger is Explicit.

I added a handler to the button's click, now what?

How do I collect the info from the controls? they don't have a key they are dynamic, how do I get their 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>
+1  A: 
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

Update
In some scenarios, BindingGroup would be the best solution, then U call BindingGroup.UpdateSources.

Shimmy
Thanks for the update about BindingGroup. Helps a lot!
Scott