C# TextBox应以特定格式显示十六进制文本

C# TextBox应以特定格式显示十六进制文本,c#,wpf,mvvm,textbox,C#,Wpf,Mvvm,Textbox,我的xaml文件中有一个可编辑的文本框。现在,根据我的项目要求,textbox中的内容应仅为0-9和a-f(十六进制值),textbox应根据十六进制值进行输入 演示: 12 ab 32 A564 现在,如果我的光标在末尾,我继续按backspace,它应该像在常规文本框中一样删除这些值 现在,如果我的光标位于a5的开头,并且我按下“delete键”,则该值应如下所示: 12 ab 32 56 4 如果我的光标位于a5的末尾,并且我按下“删除键”,则不会发生任何事情 我在C++应用程序中成功完成

我的xaml文件中有一个可编辑的文本框。现在,根据我的项目要求,textbox中的内容应仅为0-9和a-f(十六进制值),textbox应根据十六进制值进行输入

演示:

12 ab 32 A564

现在,如果我的光标在末尾,我继续按backspace,它应该像在常规文本框中一样删除这些值

现在,如果我的光标位于a5的开头,并且我按下“delete键”,则该值应如下所示:

12 ab 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);
        }
}
void CMSP430CommPanel::textEditorExtChanged(TextEditor&editor)
{
if(&editor==m_texti2cWrite)
{       
整数计数=0;
int定位;
字符串text1=m_texti2cWrite->getText();
String text=m_texti2cWrite->getText().removeCharacters(“”);
字符串hexString=String::empty;
int countCaret=m_texti2cWrite->getCaretPosition();
对于(int i=0;isetText(十六进制字符串,false);
如果(text1.length()==m_texti2cWrite->getCaretPosition())
{
m_texti2cWrite->setCaretPosition(计数);
}
其他的
{
m_texti2cWrite->setCaretPosition(countCaret);
}
}
}

其中m_texti2cWrite是textbox的名称。我如何在基于MVVM的wpf应用程序中实现相同的情况。我有一个textbox,它应该像上面所说的那样接受输入。请帮助!!!

尝试使用扩展wpf工具包。 抱歉,我在
MaskedTextBox
中仔细查看了可能的掩码值。十六进制数没有掩码字符。(

您应该取消对答案的标记。
我在扩展WPF工具包的跟踪器上发布了一条消息。

尝试使用扩展WPF工具包。 抱歉,我在
MaskedTextBox
中仔细查看了可能的掩码值。十六进制数没有掩码字符。(

您应该取消对答案的标记。

我在Extended WPF Toolkit's tracker上发布了一篇文章。

因为您使用MVVM-您可以通过a来实现这一点-我尝试过这样做,主要是出于好奇-这似乎工作得很好,但目前每个控件需要一个转换器实例,因为它使用一个实例变量来缓存最后一个已知的好十六进制值-我确信您可以ld将其与验证结合使用以改进它

更新 好的,这似乎有效(ish)-只允许1-9和A-F,我不得不禁用文本框选择,因为它会导致奇怪的结果-我使用了附加的行为来控制光标,可能有更好的方法来实现这一点,但我确实不知道如何

删除行为按照您的要求工作(如果在一对的末尾删除,则不会执行任何操作)

有戏:)

更新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 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);
    }
}
文本框行为

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);
    }
}

由于您使用的是MVVM-您可以通过a来实现-我尝试过这样做,主要是出于好奇-这似乎工作得很好,但目前每个控件需要一个转换器实例,因为它使用一个实例变量来缓存最后一个已知的好十六进制值-我确信您可以将其与验证结合使用来改进它

更新 好的,这似乎有效(ish)-只允许1-9和A-F,我不得不禁用文本框选择,因为它会导致奇怪的结果-我使用了附加的行为来控制光标,可能有更好的方法来实现这一点,但我确实不知道如何

删除行为按照您的要求工作(如果在一对的末尾删除,则不会执行任何操作)

有戏:)

更新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 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);
    }
}
文本框行为

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。我必须用C#重写逻辑吗?或者他们还有其他方法吗?@OwaisWani:你必须为
MaskedTextBox
控件设置输入掩码,并将控件的
文本
与数据源的某些属性绑定。其余的(输入格式)控件会自己做。@OwaisWani:另一种方法是验证。你可以使用RegularExpressionAttribute和
IDataErrorInfo
:谢谢Dennis。我会尝试一下:)验证只是说输入是对是错,但不会改变它。但是你可以在转换器中处理输入。我从来没有使用过MaskedTextBox。我会吗要重写C#中的逻辑吗?或者他们还有其他方法吗?@OwaisWani:你必须为
MaskedTextBox
控件设置一个输入掩码,并将控件的
Text
与数据源的某些属性绑定。其余的(输入格式)控件会自己做。@OwaisWani:另一种方法是验证。你可以使用RegularExpressionAttribute和
IDataErrorInfo
:谢谢Dennis。我会尝试一下:)验证只是说输入是对是错,但不会改变它。但是你可以在转换器中操作输入。很棒的Richard。虽然我没有尝试过但是,它是否成功地执行了我在上述问题中提到的所有关键操作???我已经尝试过了,它看起来相当不错-比我现在做的要好