【问题标题】:Unable to edit TextBox in a UserControl WPF无法在 UserControl WPF 中编辑 TextBox
【发布时间】:2016-12-10 06:01:41
【问题描述】:

我正在开发一个 WPF 应用程序,其中用户控件在运行时加载到主窗口。

我在 UserControl 中有一个 TextBox,它应该是一个可编辑的 TextBox。但我无法使用键盘输入对其进行编辑。然而,我能够务实地设置文本。

我已就该问题转介MSDN forum。我仍然无法通过。我的代码类似于上面链接中的代码。

请分享您的想法以完成这项工作。

提前致谢。

编辑:

xml代码:

<UserControl x:Class="UserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d"
         Loaded="UserControl_Loaded"
         FocusManager.IsFocusScope="True"
         FocusManager.FocusedElement="{Binding ElementName=TestTxt}">

<Grid FocusManager.IsFocusScope="True">
    <StackPanel Orientation="Horizontal" FocusManager.IsFocusScope="True">
        <TextBox x:Name="TestTxt" IsReadOnly="False" IsEnabled="True" FocusManager.IsFocusScope="True"/>
    </StackPanel>
</Grid>

【问题讨论】:

  • 你能解释一下吗。 “无法输入”是指它被禁用了吗?检查文本框的IsEnable 属性。
  • 在此处发布您的代码
  • @Emad:它没有被禁用。它不需要键盘输入。我已确保文本框已启用且 'IsReadOnly' 设置为 false。
  • 我想到的另一件事是Text 属性绑定到另一个不允许更改的属性。您需要发布一些代码。
  • @Emad:我没有在代码中的任何地方使用“文本”属性。仅出于测试目的,我使用了该属性。

标签: c# wpf xaml textbox user-controls


【解决方案1】:

UserControl_Loaded 中添加TestTxt.Focus();

【讨论】:

  • 仍然面临@swiszcz 的问题
  • 那么你需要在这里发布一个完整的例子。我创建了一个新的 WPF 应用程序项目,添加了一个用户控件,其中包含从您那里复制的 xaml,并且在 UserControl_Loaded 中添加了 TestTxt.Focus() 之后(在 MainWindow.xaml 中我添加了 FocusManager.FocusedElement="{Binding ElementName=TheNameOfUserControl}") 在应用程序启动时我可以在文本框中输入,
  • 我找到了这个问题的根本原因。这是一个媒体流应用程序。由于视频流,我无法更新 UI。有什么办法可以做到吗?
  • 这样可以解决问题,但是如果您有多个文本框,那么您仍然无法在其他文本框上单击和输入,只有您聚焦的那个可以被编辑。
猜你喜欢
  • 2011-05-12
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 2011-03-08
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
相关资源
最近更新 更多