我有一个StackPanel控件。
当我右键单击该面板时,会出现一个上下文菜单,我可以编辑这些控件的文本。
在此处,我使用TextBlock来显示数据,并使用TextBox来编辑数据(当TextBox可见时,TextBlock变为折叠状态,反之亦然)
我希望在TextBox可见时选择所有文本并将其聚焦。
当我右键单击该面板时,会出现一个上下文菜单,我可以编辑这些控件的文本。
在此处,我使用TextBlock来显示数据,并使用TextBox来编辑数据(当TextBox可见时,TextBlock变为折叠状态,反之亦然)
我希望在TextBox可见时选择所有文本并将其聚焦。
我尝试使用Interaction,但没有成功:(
有没有办法做到这一点?
例如:当TextBox可见时,我可以在我的ViewModel中触发某些命令,并从我的ViewModel中选择所有文本。
<TextBlock Text="{Binding MachineResponseText}" Visibility="{Binding IsEditing, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=true}"/>
<TextBox x:Name="MachineResponseTextBox" Text="{Binding MachineResponseText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding IsEditing, Converter={StaticResource BoolToVisibilityConverter}}">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsVisible" Value="True">
<!--Is there any way to select all the text when this textbox is visible?-->
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
TextBox
中的所有文本,因此你的VM
应该不涉及此事。在我看来,这听起来像是一个代码后台解决方案,因为VM
并不真正关心文本是否被选中。除非我误解了你的意思? - XAMlMAXTextBox
有一个SelectedText
属性,你可以在gotFocus
事件上设置它。但如果这只是在编辑模式之间切换,为什么不操作TextBox
上的IsEnabled
属性呢? - XAMlMAX