我有一些代碼來設置文本框的集中屬性,但我實際上是在發現如果當前文本框有鍵盤焦點,我需要從我的視圖模型獲取附加屬性的值而不是設置它
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)
);
public static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uie = (UIElement)d;
if ((bool)e.NewValue)
{
uie.Focus();
}
}
}
確定這和XAML是
源 code的<TextBox Text="{Binding Path=ClientCode}" c:FocusExtension.IsFocused="{Binding IsClientCodeFocused}" />
通過此附加屬性,您可以綁定到布爾型DependencyProperty或INPC屬性,以便在屬性更改時設置焦點。 **你在哪裏尋找它的焦點?** – Will
@將從我的viewmodel – Rob
您的ViewModel不在乎什麼是重點。這是View的責任。 [你的問題是無效的。](http://i.stack.imgur.com/PBUhJ.jpg)(抱歉,不想花時間從「旗」改爲「問題」)。我認爲你的真正問題應該是你正在努力完成的任務:你對什麼是和沒有關注的控制,以及如何在不違反MVVM的情況下做到這一點。 – Will