8,756
社区成员




private static void OnUpdateSourceTriggerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextBox textBox = d as TextBox;
if ((bool)e.OldValue)
{
textBox.TextChanged -= (s, arg) => {
};
}
if ((bool)e.NewValue)
{
textBox.TextChanged += (s, arg) => {
var c = findFocusableControl(textBox);
if (c != null)
{
c.Focus();
}
textBox.Focus();
};
}
}
private static Control findFocusableControl(Control control)
{
var ctl = VisualTreeHelper.GetParent(control);
if ((ctl as Control) != null)
{
return ctl as Control;
}
else
{
int childrenCount = VisualTreeHelper.GetChildrenCount(ctl);
for (int i = 0; i < childrenCount; i++)
{
var c = VisualTreeHelper.GetChild(ctl, i) as Control;
if ((c != null) &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; (c != control))
{
return c;
}
}
}
return null;
}
}
<TextBox x:Name="nameTextbox" Height="25" Width="100" Margin="5" Text="{Binding Name, Mode=TwoWay}"
local:UpdateSourceTriggerHelper.UpdateSourceTrigger="True" />