关于如何使用UpdateSourceTrigger =显式MVVM的一个很好的例子 [英] A good example on how to use UpdateSourceTrigger=Explicit with MVVM
问题描述
我有以下形式:
< StackPanel x:Name =LayoutRootMargin =10DataContext ={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 =用户名: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 =用户密码:VerticalAlignment =Center/>
< TextBox Text ={Binding User.Password,Mode = TwoWay,UpdateSourceTrigger = Explicit}Margin =26,0,0,0/>
< / DockPanel>
< StackPanel Orientation =HorizontalVerticalAlignment =CenterHorizontalAlignment =RightMargin =0,5,0,0>
< Button Content =OkIsDefault =TrueWidth =70Margin =0,0,15,0Click =Ok_Click/>
< Button Content =取消IsCancel =TrueWidth =70/>
< / StackPanel>
< / StackPanel>
我应该调用哪种方法来更新用户
属性?
我不想通过x:Name来调用元素来调用绑定。如果我必须通过x:Name处理这些元素,就我而言,我也可以没有约束力。
您需要调用 BindingExpression.UpdateSource 后面的代码手动更新绑定。显式绑定与MVVM并不真正兼容,因为您需要直接引用视图对象来执行手动更新。
// itemNameTextBox是TextBox的一个实例
BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
I'm trying to figure out how to use UpdateSourceTrigger=Explicit.
I've got following form:
<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>
what method should I call to update User
property?
I don't want to address the elements by x:Name to invoke the binding. If I have to address the elements by x:Name, I as well can go without binding altogether, as far as I'm concerned.
You need to call BindingExpression.UpdateSource in the code behind to manually update the binding. Explicit binding isn't really compatible with MVVM since you need to directly reference the view objects to perform the manually update.
// itemNameTextBox is an instance of a TextBox
BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
这篇关于关于如何使用UpdateSourceTrigger =显式MVVM的一个很好的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!