2017-12-07 7 views
0

WPF 창에서 텍스트 상자 중 하나에 커서를 놓으려고합니다. 는 몇 가지 질문과 답변을 읽은 후, 나는 시도 다음wpf MVVM 포커스 커서를 텍스트 상자에 넣으십시오.

<StackPanel Grid.Row="1" 
    <StackPanel.Style> 
     <Style> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding UserShouldEditValueNow}" Value="true"> 
        <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=FID}"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </StackPanel.Style> 
    <TextBox Name ="FID" Text="{Binding FixID, UpdateSourceTrigger=PropertyChanged}" 
    FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/> 
</StackPanel> 

CS : (뷰 모델)

this.UserShouldEditValueNow = true; 

내가 텍스트 상자 FID에 깜박이는 커서를 볼 것으로 예상 XAML은

창을 열 때. 그러나이 텍스트 상자에는 커서가 없습니다. 디버깅은 cs 코드를 통해 값을 true로 설정한다는 것을 보여주었습니다. 이유가 무엇입니까?

+0

대답을 시도 [이 (https://stackoverflow.com/a/1356781/6869276) 방식 .xaml

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.Dispatcher.BeginInvoke( new Action( delegate{ uie.Focus(); Keyboard.Focus(uie); } ) ); } } } 

. 이 더불어'FocusExtension' 클래스를 추가 StackPanel의 제거 및 이동 : '<텍스트 상자의 텍스트 = "텍스트" \t \t 지역 : FocusExtension.IsFocused = "{UserShouldEditValueNow 바인딩}"/> 나를 위해' 작품. – pixela

+0

가능한 [WPF에서 텍스트의 포커스를보기 모델 (C#)에서 설정] (https://stackoverflow.com/questions/1356045/set-focus-on-textbox-in-wpf-from-view-model-c)) – techvice

답변

0

솔루션에는 다음이 포함됩니다. 1. FocusExtension 클래스 추가. 2. Focus 및 Keyboard.Focus는 Dispatcher.BeginInvoke 내에 있습니다.

cs.

 <TextBox Text="{Binding FixID, UpdateSourceTrigger=PropertyChanged}" viewModels:FocusExtension.IsFocused="{Binding UserShouldEditValueNow}" />