WPF TextBox.SelectAll()不起作用

Zol*_*iqa 3 textbox selectall datatemplate wpf-controls

我在项目中使用了以下模板:

<DataTemplate 
    x:Key="textBoxDataTemplate">
    <TextBox 
        Name="textBox"
        ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"
        Tag="{Binding}"
        PreviewKeyDown="cellValueTextBoxKeyDown">
        <TextBox.Text>
            <MultiBinding
                Converter="{StaticResource intToStringMultiConverter}">
                <Binding 
                    Path="CellValue"
                    Mode="TwoWay">
                        <Binding.ValidationRules>
                            <y:MatrixCellValueRule 
                                MaxValue="200" />
                        </Binding.ValidationRules>
                </Binding>
                <Binding 
                    RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type y:MatrixGrid}}" 
                    Path="Tag"
                    Mode="OneWay" />
            </MultiBinding>
        </TextBox.Text>
    </TextBox>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

我使用此模板为用户创建可编辑的矩阵.用户能够在矩阵内从一个单元格导航到另一个单元格,我想突出显示所选文本框中的数据,但它不起作用.我调用TextBox.Focus()和TextBox.SelectAll()来实现效果,但没有.Focus()有效但文本永远不会突出显示.

任何帮助都是受欢迎和赞赏的.

Zol*_*iqa 13

好吧,如果有人感兴趣,我的这个问题的解决方案是e.Handled = true;在事件处理程序方法中包含语句,其中调用textBox.SelectAll()textBox.Focus()调用.

问题是,我附上一个事件处理文本框的PreviewKeyDown事件,处理一个隧道事件,可能是SelectAll()Focus()调用,而不调用忽略e.Handled = true;声明.

希望它能帮助别人.