<UserControl UIHelper:FocusExtension.IsFocused="{Binding FocusUserControl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Focusable="True" IsTabStop="True">
在这里,我添加了一个IsFocused道具来保持用户控制的重点
public static class FocusExtension
{
public static bool GetIsFocused(DependencyObject obj)
{
return (bool)obj.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject obj, bool value)
{
obj.SetValue(IsFocusedProperty, value);
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused", typeof(bool), typeof(FocusExtension),
new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));
private static void OnIsFocusedPropertyChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var uie = (UIElement)d;
if ((bool)e.NewValue)
{
uie.Focus();
}
}
}
这是我的FocusExtension类
public bool FocusUserControl
{
get { return focusUserControl; }
set
{
focusUserControl = value;
OnPropertyChanged(new PropertyChangedEventArgs("FocusUserControl"));
}
}
这是我的VM道具。已绑定,现在在用户控制中,我有一个按钮。当我单击它时,将新用户控件。那时打开我需要将FocusUserControl
更新为false。 不再关注该用户控件。我被困在这里,请提出一些感谢您的帮助。
**
更新我添加了类似的内容
**
private static void OnIsFocusedPropertyChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
uie = (UIElement)d;
if ((bool)e.NewValue)
{
uie.LostKeyboardFocus += LostFocus;
uie.Focus();
}
}
static void LostFocus(object sender, RoutedEventArgs e)
{
uie.LostKeyboardFocus -= LostFocus;
// Here i need to update `FocusUserControl` to false
}
在LostFocus中,我只需要将FocusUserControl
更新为false。我该怎么做?真的需要一些帮助。