控件中数据模板禁用小键盘减号折叠

WPF里面自定义了TreeListView控件,在树列表项的数据模板中添加了文本框,当在文本框内输入小键盘的减号时,自动折叠;因为小键盘+与-符号是自动折叠的;可以对win7及以上系统 的文件夹列表树,进行验证;为了禁止输入时不折叠,使用以下代码实现

<controls:TreeListView.Columns>

   <GridViewColumn Header="列头名称">

    <DataTemplate>

        <TextBox Text="{Binding Name}" PreviewkeyDown="textBox_PreviewkeyDown"/>

    </DataTemplate>

   </GridViewColumn>

</controls:TreeListView.Columns>

后台代码

private void textBox_PreviewkeyDown(object sender,KeyEventArgs e)

{

  if(e.KeyStates==Keyborad.GetKeyStates(Key.Subtract))

  {

    TextBox t1=sender as TextBox;

    int index = t1.SelectionStart; //当前光标位置

    int sectionLength = t1.SelectionLength;//选中的字符长度

    string tempStr = t1.Text.Remove(index,sectionLength);    

    t1.Text = tempStr.Insert(index,"-");

    e.Handled = True;//禁用小键盘减号折叠

  }

}

原文地址:https://www.cnblogs.com/yxhblog/p/7087256.html