Giống như Pieter đề nghị tôi giải quyết nó bằng một lớp kế thừa như thế này:
public class ActiveTextBox:TextBox
{
public ActiveTextBox()
{
Loaded += ActiveTextBox_Loaded;
}
void ActiveTextBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
Binding myBinding = BindingOperations.GetBinding(this, TextProperty);
if (myBinding != null && myBinding.UpdateSourceTrigger != UpdateSourceTrigger.PropertyChanged)
{
Binding bind = (Binding) Allkort3.Common.Extensions.Extensions.CloneProperties(myBinding);
bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(this, TextBox.TextProperty, bind);
}
}
}
và helpmethod này:
public static object CloneProperties(object o)
{
var type = o.GetType();
var clone = Activator.CreateInstance(type);
foreach (var property in type.GetProperties())
{
if (property.GetSetMethod() != null && property.GetValue(o, null) != null)
property.SetValue(clone, property.GetValue(o, null), null);
}
return clone;
}
Bất kỳ đề nghị làm thế nào để giải quyết nó tốt hơn?
Tôi sẽ cố gắng giải kiến kiến đó nhờ thông tin. – Cinaird