6

我试图弄清楚如何使用 UpdateSourceTrigger=Explicit。

我有以下表格:

<StackPanel x:Name="LayoutRoot" Margin="10" DataContext="{Binding ElementName=Window, Mode=OneWay}">
    <DockPanel>
        <TextBlock Text="Proxy address:" VerticalAlignment="Center"/>
        <TextBox Text="{Binding User.PageAddress, Mode=TwoWay, UpdateSourceTrigger=Explicit}" Margin="28,0,0,0"/>
    </DockPanel>
    <DockPanel Margin="0,5,0,0">
        <TextBlock Text="User name:" VerticalAlignment="Center"/>
        <TextBox Text="{Binding User.UserName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" Margin="46,0,0,0"/>
    </DockPanel>
    <DockPanel Margin="0,5,0,0">
        <TextBlock Text="User password:" VerticalAlignment="Center"/>
        <TextBox Text="{Binding  User.Password, Mode=TwoWay, UpdateSourceTrigger=Explicit}" Margin="26,0,0,0"/>
    </DockPanel>
    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,5,0,0">
        <Button Content="Ok" IsDefault="True" Width="70" Margin="0,0,15,0" Click="Ok_Click"/>
        <Button Content="Cancel" IsCancel="True" Width="70"/>
    </StackPanel>
</StackPanel>

我应该调用什么方法来更新User属性?

我不想通过 x:Name 来处理元素来调用绑定。如果我必须通过 x:Name 来处理元素,就我而言,我也可以完全不绑定。

4

1 回答 1

10

您需要在后面的代码中调用BindingExpression.UpdateSource来手动更新绑定。显式绑定与 MVVM 并不真正兼容,因为您需要直接引用视图对象来执行手动更新。

// itemNameTextBox is an instance of a TextBox
BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
于 2012-05-01T06:15:33.223 回答