简体   繁体   English

WPF设置重点问题

[英]WPF Setting focus Issues

Basis: I am using the MVVM pattern or a subset of it. 基础:我正在使用MVVM模式或其一部分。

In my main window I have a button that opens up a usercontrol with a new DataContext the function looks kinda like this: 在我的主窗口中,有一个按钮可以打开带有新DataContext的用户控件,该函数看起来像这样:

public void SetUserControl()
{
     UCDatacontext = new UCViewModel(this);
     base.OnPropertyChanged("UCDatacontext");
     UCViewVisibilty = Visibility.Visible;
     UCDatacontext.IniFocus(); 
}

And then when I am done I close the usercontrol and Dispose of the DataContext. 然后,当我做我关闭用户控件和DataContext的处置。 Now the problem I am having is that I can't seem to get the focus setting to work properly, I have a Textbox in the usercontrol that I want to set focus to when the view becomes Visible. 现在我遇到的问题是我似乎无法使焦点设置正常工作,在用户控件中有一个文本框,当视图变为可见时,我想将焦点设置为该文本框。 However on the first time that I attempt to set focus it only fills the text box with an unblinking caret, which after investigation leads me to believe that it is because the TB isn't getting the Keyboard focus (only logical focus), however even after explicity setting the keyboard focus I still get the unblinking caret, and it is only after clicking in the TB that it is getting focus. 但是,在我第一次尝试设置焦点时,它只会用不闪烁的插入符号填充文本框,经过调查,我认为这是因为TB没有获得键盘焦点(仅是逻辑焦点),尽管如此明确设置键盘焦点后,我仍然会看到闪烁的插入符号,只有在TB中单击后,它才能获得焦点。 The method I am using to set focus is similar to method described here . 我用于设置焦点的方法与此处描述的方法类似。
If in the view I do some writeline debugging by printing out in the FocusSet Event for the textbox it does get set, however only on the first time I call SetUserControl(). 如果在视图中通过在FocusSet事件中打印出文本框来进行写线调试,则它确实会被设置,但是仅在我第一次调用SetUserControl()时才设置。 If I call SetUserControl() again it does nothing, except making the View Visible but doesn't trigger the Focus Set Event. 如果我再次调用SetUserControl(),除了使“视图可见”但不触发“焦点设置事件”外,它什么都不做。
Below is the lines of code from the MainWindow: 下面是MainWindow中的代码行:

<Grid Grid.ColumnSpan="5" Grid.RowSpan="5" Visibility="{Binding Path=UCViewVisibilty }" x:Name="UCGrid"   >
    <Grid.Effect>
        <DropShadowEffect  />
    </Grid.Effect>
    <View:UCView DataContext="{Binding Path=UCDatacontext}"   />    
</Grid>

And UserControl Grid: 和UserControl网格:

<Grid > 
    <TextBox Uid="UCTB" localExtensions:FocusExtension.IsFocused="{Binding Path=UCTBFocus}" Height="23" HorizontalAlignment="Left" Margin="113,56,0,0" Name="UCTB" VerticalAlignment="Top" Width="165" Text="{Binding Path=UCTBContent, UpdateSourceTrigger=PropertyChanged}" GotFocus="UCTB_GotFocus" />
</Grid >

The Focus is set in the UserControlViewModel, and is set after the Usercontrol is rendered. Focus在UserControlViewModel中设置,并且在呈现Usercontrol之后设置。

it like this Set focus one by one from top to bottom. 像这样将焦点从上到下逐一设置。

InitializeComponent();
        FocusManager.SetFocusedElement(this, TabItem); //this is Window , TabItem is UserControl in this Window
        FocusManager.SetFocusedElement(TabItem, TextBox); // TabItem is UserControl and TextBox is Control in TabItem UC

I hope this will help. 我希望这将有所帮助。

As it turns out, after fiddling around with the code, the reason why the focus wasn't being set properly in the View was because the binding in the View Model was this: 事实证明,在摆弄代码之后,未在View中正确设置焦点的原因是因为View Model中的绑定是这样的:

    bool _tBfocus;
    public bool UCTBFocus
    {
        get { return _tBfocus; }
        set
        {
            _tBfocus= value;
            base.OnPropertyChanged("UCTBFocus");

       }

instead of: 代替:

    bool _tBfocus;
    public bool UCTBFocus
    {
        get { return _tBfocus; }
        set
        {
            if (_tBfocus == value)
                return;
            _tBfocus= value;
            base.OnPropertyChanged("UCTBFocus");

        }
    }

After changing it everything worked fine :/ but if someone could explain to me why this annoyance I was having was caused by that I would be truly grateful :) 更改之后,一切正常:/但是,如果有人可以向我解释为什么造成这种烦恼,我将不胜感激:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM