如何在ListBox等Items控件中使用UpdateSourceTrigger = Explicit

时间:2009-08-23 04:27:05

标签: wpf xaml binding triggers

我有一个ListBox,每个项目都有一个按钮,我在数据项目中设置了Binding.UpdateSourceTrigger显式的所有文本框。

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

如何从控件中收集信息?他们没有关键,他们是动态的,我怎么得到他们的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 个答案:

答案 0 :(得分: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