在TextBox上设置转换器

时间:2013-07-03 18:17:41

标签: c# wpf

我需要计算/更改文本框与其可绑定源之间的输入形式。我试图实现这一目标的方式是在转换器的帮助下。

转换器:

    public class ParameterConverter : IValueConverter
{

    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return string.Empty;

        string originalParameValue = value.ToString();

        string fixedParameterValue = string.Format("@_{0}", originalParameValue);

        return fixedParameterValue;
    }

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }
}

XAML:

    <Window.Resources>
    <converters:ParameterConverter x:Key="parameterConverter"  />
</Window.Resources>

<Grid>
    <TextBox Text="{Binding ParameterA, Converter={StaticResource parameterConverter}}"/>
</Grid>

问题是,转换器只运行一次。它接近正确(我的意思是转换器)还是有其他方法?

2 个答案:

答案 0 :(得分:0)

也许绑定模式不是双向的,并且你的属性fire属性已经改变了。

答案 1 :(得分:0)

您的数据上下文是否实现了INotifyPropertyChanged并且每当ParameterA发生更改时都会调用PropertyChanged?似乎没有人通知文本框需要更新其内容。

相关问题