簡體   English   中英

覆蓋文本框的文本屬性

[英]Override text property of textbox

如何覆蓋WPF中文本框中的文本屬性?

我想在WPF中使用此代碼:

 public override string Text
 {
        get { return Text.Replace(",", ""); }
        set { Text = value; }
 }

如果您是對TextBox.Text屬性的數據綁定,那么另一種可能的方法是將邏輯從控件本身移開並將其放在轉換器中。 你的轉換器看起來像這樣......

public class CommaReplaceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, 
                          object parameter, CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, 
                              object parameter, CultureInfo culture)
    {
        return value.ToString().Replace(",", "");
    }
}

和數據綁定這樣的東西......

<TextBox Text="{Binding XXX, Converter={StaticResource CRC}" />

...靜態資源定義為......

<Window.Resources>
    <CommaReplaceConverter x:Key="CRC"/>
</Windows.Resources>

Text是依賴屬性。 如果從TextBox派生了一個類,則需要覆蓋元數據並提供驗證回調

看到

雖然upvoted答案是正確的,但有一個更簡單的方法:因為TextBox的Text屬性綁定到底層類的屬性(通常在Viewmodel中),只需要在底層類中提供替換。

你剛剛錯過了文本參數?

public override string Text 
    { 
        get { return Text.Replace(",", Text); } 
        set { Text = value; } 
    } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM