0
我在WP7.1中創建MaskedTextBox因爲我想將提示字符設置爲'#'。無法從WP7.1中的文本'#'創建'System.Char'
例如:## - ## - #### ##:##。雖然運行此我得到一個錯誤消息
無法創建從文本「#」
請任何人都可以幫我「System.Char」 ....
我在WP7.1中創建MaskedTextBox因爲我想將提示字符設置爲'#'。無法從WP7.1中的文本'#'創建'System.Char'
例如:## - ## - #### ##:##。雖然運行此我得到一個錯誤消息
無法創建從文本「#」
請任何人都可以幫我「System.Char」 ....
請,後關於您的例外的更多細節。 此外,考慮使用標準PasswordBox與PasswordChar屬性:
<PasswordBox PasswordChar="#"/>
UPDATE:
使用您PromptChar屬性與此字符轉換器:
public class CharTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value == null)
return '_';
if (value is string)
{
string s = (string)value;
if (s.Length == 0 || s.Length > 1)
return '_';
return s[0];
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value != null && !(value is char))
throw new ArgumentException("Invalid prompt character", "value");
if (destinationType == typeof(string))
{
if (value == null)
return String.Empty;
char promptChar = (char)value;
return promptChar.ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
用法:
[TypeConverter(typeof(CharTypeConverter))]
public char PromptChar
{
get { ... }
set { ... }
}
感謝您的回覆。我沒有使用PasswordBox。我正在用TextBox的基類創建自定義MaskTextBox。我有一個名爲** PromptChar **的依賴屬性,類型爲char。如果我給 。然後在運行時獲取提到的錯誤, –
Ponmalar
2012-03-22 06:49:55
您是否調試過 - 將哪種值發送到依賴項屬性?請發佈您的MaskedTextBox類的相關實現。 – 2012-03-22 06:51:30
是的,僅將值設爲'#'。具有以下財產。公共字符PromptChar { get {return(char)GetValue(PromptCharProperty); } {SetValue(PromptCharProperty,value);} set {SetValue(PromptCharProperty,value); } } //使用DependencyProperty作爲PromptChar的後備存儲。這使得動畫,樣式,綁定等... public static readonly DependencyProperty PromptCharProperty = DependencyProperty.Register(「PromptChar」,typeof(char),typeof(MaskEditTextBox),new PropertyMetadata('_',OnPromptCharChanged));在OnPromptCharChanged事件中, – Ponmalar 2012-03-22 06:53:48