0

我想寫一個String.Forat字母表,你可以請告訴如何寫它我知道貨幣的模式,但不知道字母表。我可以寫字母StringFormat = "{}{A-Z,a-z}"嗎?什麼是字符串在WPF中的字符格式?

StringFormat="{}{0:C}" 

我做validation.I只要輸入字母TextBox中需要較低的信。當我輸入任何數字值,那麼它告訴我一個錯誤的第一個字母大寫休息,但使用任何WPF驗證,但我不知道該怎麼辦使用字母

+1

我們需要知道你打算使用'StringFormat'什麼。 –

+1

聽起來像你想正則表達式不'StringFormat' –

+0

我如何使用正則表達式爲此,我真的堅持在這... – user1050667

回答

1
public class MyTextBox: TextBox 
{ 
    public MyTextBox() 
{ 
    this.PreviewTextInput += new TextCompositionEventHandler(TextBox_PreviewTextInput); 
    this.AddHandler(DataObject.PastingEvent, new DataObjectPastingEventHandler(OnPaste)); 
} 

private void OnPaste(object sender, DataObjectPastingEventArgs e) 
{    
    if (e.DataObject.GetDataPresent(typeof(String))) 
    { 
     String text = (String)e.DataObject.GetData(typeof(String)); 
     if (!IsTextAllowed(text)) 
     { 
      e.CancelCommand(); 
     } 
    } 
    else 
    { 
     e.CancelCommand(); 
    } 
} 

void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) 
{ 
    e.Handled = !IsTextAllowed(e.Text); 
} 

private static bool IsTextAllowed(string text) 
{ 
    Regex regex = new Regex("^[a-zA-Z]+$"); //regex that matches disallowed text 
    return regex.IsMatch(text); 
} 
} 

的的StringFormat使用customTextBox像上面