<legend id='acOhA'><style id='acOhA'><dir id='acOhA'><q id='acOhA'></q></dir></style></legend>
    1. <tfoot id='acOhA'></tfoot>
      <i id='acOhA'><tr id='acOhA'><dt id='acOhA'><q id='acOhA'><span id='acOhA'><b id='acOhA'><form id='acOhA'><ins id='acOhA'></ins><ul id='acOhA'></ul><sub id='acOhA'></sub></form><legend id='acOhA'></legend><bdo id='acOhA'><pre id='acOhA'><center id='acOhA'></center></pre></bdo></b><th id='acOhA'></th></span></q></dt></tr></i><div id='acOhA'><tfoot id='acOhA'></tfoot><dl id='acOhA'><fieldset id='acOhA'></fieldset></dl></div>
          <bdo id='acOhA'></bdo><ul id='acOhA'></ul>

        <small id='acOhA'></small><noframes id='acOhA'>

      1. WPF MVVM 负载上的焦点字段

        时间:2023-06-09

        <i id='LKUrq'><tr id='LKUrq'><dt id='LKUrq'><q id='LKUrq'><span id='LKUrq'><b id='LKUrq'><form id='LKUrq'><ins id='LKUrq'></ins><ul id='LKUrq'></ul><sub id='LKUrq'></sub></form><legend id='LKUrq'></legend><bdo id='LKUrq'><pre id='LKUrq'><center id='LKUrq'></center></pre></bdo></b><th id='LKUrq'></th></span></q></dt></tr></i><div id='LKUrq'><tfoot id='LKUrq'></tfoot><dl id='LKUrq'><fieldset id='LKUrq'></fieldset></dl></div>

          <small id='LKUrq'></small><noframes id='LKUrq'>

          <tfoot id='LKUrq'></tfoot>

            • <bdo id='LKUrq'></bdo><ul id='LKUrq'></ul>
                  <tbody id='LKUrq'></tbody>

                • <legend id='LKUrq'><style id='LKUrq'><dir id='LKUrq'><q id='LKUrq'></q></dir></style></legend>
                  本文介绍了WPF MVVM 负载上的焦点字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个视图,它有一个 TextBox 和下面的几个 Button.当窗口加载时,我希望 TextBox 具有焦点.

                  I have a View that has a single TextBox and a couple Buttons below it. When the window loads I want that TextBox to have focus.

                  如果我不使用 MVVM,我只会在 Loaded 事件中调用 TextBox.Focus().但是,我的 ViewModel 不知道我的视图,所以我如何在不将代码放入视图的代码隐藏的情况下完成此操作?

                  If I was not using MVVM I would just call TextBox.Focus() in the Loaded event. However my ViewModel does not know about my view so how can I accomplish this without putting code into the codebehind of my view?

                  阅读答案后,我决定将此代码放在视图 xaml 中

                  After reading the answers I decided to put this code in the view xaml

                  <DockPanel FocusManager.FocusedElement="{Binding ElementName=MessageTextBox}">    
                      <TextBox Name="MessageTextBox" Text="{Binding Message}"/>
                  </DockPanel>
                  

                  如果这不是初始页面焦点,我可能会推荐 Jon Galloway 的答案,因为它可以从 ViewModel 控制.

                  If this were anything other than initial page focus I would probably recommend Jon Galloway's answer since it can be controlled from the ViewModel.

                  推荐答案

                  如果它让你感觉更好(它让我感觉更好),你可以在 Xaml 中使用附加属性来做到这一点:

                  If it makes you feel better (it makes me feel better) you can do this in Xaml using an attached property:

                  http://msdn.microsoft.com/en-us/library/system.windows.input.focusmanager.focusedelement.aspx

                  如果您知道技巧,那么您可以在代码隐藏中执行的任何操作都可以在 Xaml 中执行.幸运的是,您不必实施此技巧 - MS 为您完成了.

                  Anything you can do in codebehind you can do in Xaml if you know the tricks. Fortunately, you didn't have to implement this trick - MS did it for you.

                  这篇关于WPF MVVM 负载上的焦点字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Windows 窗体应用程序中验证文本框 下一篇:TextBox 中 Windows 窗体滚动日志输出的最佳方法

                  相关文章

                  <legend id='bYVK1'><style id='bYVK1'><dir id='bYVK1'><q id='bYVK1'></q></dir></style></legend>

                  <small id='bYVK1'></small><noframes id='bYVK1'>

                    <bdo id='bYVK1'></bdo><ul id='bYVK1'></ul>
                  <i id='bYVK1'><tr id='bYVK1'><dt id='bYVK1'><q id='bYVK1'><span id='bYVK1'><b id='bYVK1'><form id='bYVK1'><ins id='bYVK1'></ins><ul id='bYVK1'></ul><sub id='bYVK1'></sub></form><legend id='bYVK1'></legend><bdo id='bYVK1'><pre id='bYVK1'><center id='bYVK1'></center></pre></bdo></b><th id='bYVK1'></th></span></q></dt></tr></i><div id='bYVK1'><tfoot id='bYVK1'></tfoot><dl id='bYVK1'><fieldset id='bYVK1'></fieldset></dl></div>

                  1. <tfoot id='bYVK1'></tfoot>