请注意 - 我可能会在此回复中关闭:
让我试着理解你的意思:
如果我是正确的,在界面上,正在更改绑定到某个实体的值。这个实体,就是你想要在服务器上实时更新的。如果是这种情况,我会推荐这个:
假设我有一个文本框,当用户在文本框中键入值时,它会更新绑定实体,并且我希望同时在服务器上更新该实体 - 定义一个行为以采用 keyup 方法并触发例如(AssociatedObject.GetBindingExpression(TextBox.TextProperty).UpdateSource();)
此外,在这种行为中,我将赋予文本框定义绑定到 OnKeyUp 事件的方法的能力,从而允许我说,一旦用户按下“Enter”,然后实际在服务器上执行 udpate。此外,我会创建一个加载屏幕/子窗口来表示“正在保存” - 然后会导致“阻塞”发生,以防止在保存期间任何进一步的用户干预。在回电。我会关闭说加载的窗口。
现在,就像我说的,我不确定这是否是您正在寻找的答案,但这个想法背后的理论,尤其是仅在进入或最终更改时的阻止似乎是您最好的选择。文本框的行为示例如下:
包括你的图书馆:using CustomControlsUI.Extensions;
定义文本框的类:public class TTextBoxKeyUpBehavior:Behavior<TextBox>
为要动态绑定的 ontextkeyup 定义方法指针:protected MethodInfo OnTextBoxKeyUp { get; set; }
定义将 OnTextKeyUpMethod 命名为要绑定到的公共字符串:OnTextBoxKeyUpMethod
在行为中定义TextboxKeyUp事件:
/// <summary>
/// Method impl. for the onkey up event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBoxKeyUp(object sender, KeyEventArgs e)
{
if (!object.ReferenceEquals(AssociatedObject.GetBindingExpression(TextBox.TextProperty), null))
{
AssociatedObject.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
if (!string.IsNullOrEmpty(OnTextBoxKeyUpMethod) && !object.ReferenceEquals(AssociatedObject,null) &&
!object.ReferenceEquals(AssociatedObject.DataContext,null))
{
try
{
//use reflection to try and find the method being pointed to.
if (object.ReferenceEquals(OnTextBoxKeyUp, null))
{
MethodInfo _m = null;
_m = AssociatedObject.DataContext.GetType().GetMethod(OnTextBoxKeyUpMethod, new Type[] { typeof(object), typeof(KeyEventArgs) });
if (!object.ReferenceEquals(_m, null))
//set the pointer to the on text box key up method
OnTextBoxKeyUp = _m;
}
OnTextBoxKeyUp.Invoke(AssociatedObject.DataContext, new object[] { sender, e });
}
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }
}
}
覆盖 On Attached 和 On Detached 方法:
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.KeyUp += TextBoxKeyUp;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.KeyUp -= TextBoxKeyUp;
}
用法:
包括存储行为的命名空间:xmlns:ccui="clr-namespace:CustomControlsUI.Behaviors;assembly=CustomControlsUI"
包括 System.Windows.Interactivity 和 Microsoft.Expression.Interactions 的命名空间:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
在视图模型中附加 OnKeyUpTo 所需的方法:
<TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="6,4,0,0"
Name="txtFirstName" VerticalAlignment="Top" Width="200" Text="{Binding FirstName, Mode=TwoWay}">
<i:Interaction.Behaviors>
<ccui:TTextBoxKeyUpBehavior OnTextBoxKeyUpMethod="OnSearchKeyUp" />
</i:Interaction.Behaviors>
</TextBox>
在视图模型中实现应该实现在 System.ComponentModel 中找到的 INotifyPropertyChanged 的方法和属性
public string FirstName
{
get
{
return __fFirstName;
}
set
{
__fFirstName = value;
//this is a custom extension on INotifyPropertyChanged
this.NotifyPropertyChanged("FirstName", PropertyChanged);
}
}
最后定义key up方法通知服务器变化:
#region "Helper Method"
public void OnSearchKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && IsSearchStateValid==true)
{
OnFindCustomers(sender);
}
}
#endregion
请注意:我可能对这个答案不满意。但它只是基于我认为你正在寻找的东西......