我在我的XAML文件的文本框是可编辑的。 现在,根据文本框我的项目要求的内容只能是0-9和AF(十六进制值)和文本框应该基于十六进制值取输入。
Demonstratation:
12 AB 32 64 A5
现在,如果我的光标在年底,我去按Backspace键,它shud因为它在一般的文本框中发生取出值。
现在,如果我的光标在A5的开始,我按下“删除键”,该值应成为这样的:
12. 32 56 4
如果我的光标在A5的结束,我按下“删除键”没有什么应该发生。
我做了这个成功在我的C ++应用程序如下:
void CMSP430CommPanel::textEditorTextChanged (TextEditor& editor)
{
if(&editor == m_texti2cWrite)
{
int count = 0;
int location;
String text1 = m_texti2cWrite->getText();
String text = m_texti2cWrite->getText().removeCharacters(" ");
String hexString = String::empty;
int countCaret = m_texti2cWrite->getCaretPosition();
for(int i=0; i < text.length(); i++)
{
hexString = hexString + String (&text[i], 1);
if((i+1) % 2 == 0)
{
if(i != text.length()-1)
{
hexString = hexString + T(" ");
count ++;
}
}
count ++;
}
m_texti2cWrite->setText(hexString,false);
if(text1.length() == m_texti2cWrite->getCaretPosition())
{
m_texti2cWrite->setCaretPosition(count);
}
else
{
m_texti2cWrite->setCaretPosition(countCaret);
}
}
}
其中m_texti2cWrite是给文本框的名称。 我如何能实现我的WPF应用程序相同的情况下,这是基于MVVM。 我有一个文本框正如我上面所说其中shud取输入。 请帮忙!!!
由于您使用的MVVM -你可以通过做这个值转换器 -我在做这个,大多是出于好奇去-这似乎运作得很好,但目前需要每个控制转换器的实例,因为它是用一个实例变量缓存最后一次正确的十六进制值 - 我确定你可以结合使用与验证,以改善它。
更新好吧,这似乎工作(ISH) -只允许1-9和AF,我不得不禁用文本框的选择,因为它是导致奇怪的结果-我已经使用了附加的行为来控制光标,也有可能是一个更好的办法做到这一点,但我肯定不知道如何...
该删除行为可以作为你问(如果你删除在对什么都不做的结束)。
有玩:)
更新2
做了一些修改,以让它使用文本选择工作。
视图
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<local:HexStringConverter x:Key="HexConverter"></local:HexStringConverter>
</Grid.Resources>
<StackPanel>
<TextBox local:TextBoxBehaviour.KeepCursorPosition="true" VerticalAlignment="Center" Width="200" HorizontalAlignment="Center" Text="{Binding HexValue,Mode=TwoWay,Converter={StaticResource HexConverter},UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>
</Grid>
后面查看代码
public partial class MainWindow : Window
{
public MainWindow()
{
this.DataContext = new MyViewModel();
InitializeComponent();
}
视图模型
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
private string hexValue;
public string HexValue
{
get
{
return hexValue;
}
set
{
hexValue = value;
OnPropertyChanged("HexValue");
}
}
}
十六进制转换器
public class HexStringConverter : IValueConverter
{
private string lastValidValue;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string ret = null;
if (value != null && value is string)
{
var valueAsString = (string)value;
var parts = valueAsString.ToCharArray();
var formatted = parts.Select((p,i)=>(++i)%2==0 ? String.Concat(p.ToString()," ") : p.ToString());
ret = String.Join(String.Empty,formatted).Trim();
}
return ret;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
object ret = null;
if (value != null && value is string)
{
var valueAsString = ((string)value).Replace(" ",String.Empty).ToUpper();
ret = lastValidValue = IsHex(valueAsString) ? valueAsString : lastValidValue;
}
return ret;
}
private bool IsHex(string text)
{
var reg = new System.Text.RegularExpressions.Regex("^[0-9A-Fa-f]*$");
return reg.IsMatch(text);
}
}
文本框行为
public static class TextBoxBehaviour
{
public static bool GetKeepCursorPosition(DependencyObject obj)
{
return (bool)obj.GetValue(KeepCursorPositionProperty);
}
public static void SetKeepCursorPosition(DependencyObject obj, bool value)
{
obj.SetValue(KeepCursorPositionProperty, value);
}
// Using a DependencyProperty as the backing store for KeepCursorPosition. This enables animation, styling, binding, etc...
public static readonly DependencyProperty KeepCursorPositionProperty =
DependencyProperty.RegisterAttached("KeepCursorPosition", typeof(bool), typeof(TextBoxBehaviour), new UIPropertyMetadata(false, KeepCursorPosition));
public static int GetPreviousCaretIndex(DependencyObject obj)
{
return (int)obj.GetValue(PreviousCaretIndexProperty);
}
public static void SetPreviousCaretIndex(DependencyObject obj, int value)
{
obj.SetValue(PreviousCaretIndexProperty, value);
}
// Using a DependencyProperty as the backing store for PreviousCaretIndex. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PreviousCaretIndexProperty =
DependencyProperty.RegisterAttached("PreviousCaretIndex", typeof(int), typeof(TextBoxBehaviour), new UIPropertyMetadata(0));
public static string GetPreviousTextValue(DependencyObject obj)
{
return (string)obj.GetValue(PreviousTextValueProperty);
}
public static void SetPreviousTextValue(DependencyObject obj, string value)
{
obj.SetValue(PreviousTextValueProperty, value);
}
// Using a DependencyProperty as the backing store for PreviousTextValue. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PreviousTextValueProperty =
DependencyProperty.RegisterAttached("PreviousTextValue", typeof(string), typeof(TextBoxBehaviour), new UIPropertyMetadata(null));
private static void KeepCursorPosition(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var textBox = sender as TextBox;
if (textBox != null)
{
textBox.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(textBox_PreviewKeyDown);
textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);
textBox.Unloaded += new RoutedEventHandler(textBox_Unloaded);
}
else
{
throw new ArgumentException("KeepCursorPosition only available for textboxes");
}
}
static void textBox_Unloaded(object sender, RoutedEventArgs e)
{
var textBox = sender as TextBox;
textBox.PreviewKeyDown -= new System.Windows.Input.KeyEventHandler(textBox_PreviewKeyDown);
textBox.TextChanged -= new TextChangedEventHandler(textBox_TextChanged);
textBox.Unloaded -= new RoutedEventHandler(textBox_Unloaded);
}
static void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
//For some reason our e.Changes only ever contains 1 change of 1 character even if our
//converter converts it to 2 chars with the additional space - hmmm?
var textBox = sender as TextBox;
var previousIndex = GetPreviousCaretIndex(textBox);
var previousText = GetPreviousTextValue(textBox);
var previousLen = !String.IsNullOrEmpty(previousText) ? previousText.Length : 0;
var currentLen = textBox.Text.Length;
var change = (currentLen - previousLen);
var newCharIndex = Math.Max(1, (previousIndex + change));
Debug.WriteLine("Text Changed Previous Caret Pos : {0}", previousIndex);
Debug.WriteLine("Text Changed Change : {0}", change);
Debug.WriteLine("Text Changed New Caret Pos : {0}", newCharIndex);
textBox.CaretIndex = Math.Max(newCharIndex, previousIndex);
SetPreviousCaretIndex(textBox, textBox.CaretIndex);
SetPreviousTextValue(textBox, textBox.Text);
}
static void textBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
var textBox = sender as TextBox;
Debug.WriteLine("Key Preview Caret Pos : {0}", textBox.CaretIndex);
Debug.WriteLine("------------------------");
SetPreviousCaretIndex(textBox, textBox.CaretIndex);
SetPreviousTextValue(textBox, textBox.Text);
}
}
尝试使用MaskedTextBox中从扩展WPF工具包。 对不起,我看上去更专注地在可能的屏蔽值MaskedTextBox
。 还有的十六进制数没有面具字符。 :(
您应该取消标记答案。
我已经发布了一个问题,在扩展WPF工具包的跟踪。