【问题标题】:How to compute textbox value based on value and on textchanged of another如何根据值和另一个文本更改计算文本框值
【发布时间】:2015-02-23 15:11:10
【问题描述】:

我的应用程序是 WPF,我遇到了以下问题,请告诉我方向以克服它。 假设我有 3 个文本框:

<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />

所有文本框都将显示数字字符串。 textBox1.Text 在后面的代码中分配,textBox2.Text 由用户输入分配,我想要以这种方式在 textBox2 的 textChanged 上分配 textBox3.Text:

textBox3.text = string.Format("{0:0.00}", double.Parse(textBox2.Text) - double.Parse(textBox1.Text));

请告诉我如何实现这一目标?

【问题讨论】:

  • 删除所有内容并开始阅读here
  • 您面临哪些问题?
  • @HighCore,谢谢你的链接,正在阅读。
  • @Gun,问题是我想将 textBox3.Text 绑定到 textBox2 中键入的内容,但我希望目标 textBox3.Text 的输入效果。据我通过上面的链接,我需要的是绑定时的数据转换。我对吗?如果是这样,我该如何实现它?

标签: c# wpf binding


【解决方案1】:

如果你使用MVVM模式可以很容易地实现。

视图模型

public string TextBox1Text
{
    get{return _text1;}
    set{_text1 = value; NotifyPropertyChanged("ComputedProperty")}
}

public string TextBox2Text
{
    get{return _text2;}
    set{_text2 = value; NotifyPropertyChanged("ComputedProperty")}
}

public string ComputedProperty
{
    get {return string.Format("{0:0.00}", double.Parse(TextBox2Text) - double.Parse(TextBox1Text));}
}

XAML

<TextBox Text="{Binding TextBox2Text, Mode=TwoWay}" />
<TextBox Text="{Binding TextBox1Text, Mode=TwoWay}" />    
<TextBox Text="{Binding ComputedProperty}" />

【讨论】:

  • 谢谢,但我没有使用 ViewModel。
  • 在TextBox_3中寻找TextChanged事件,创建处理程序并赋值
【解决方案2】:

感谢上面来自 HighCore 的链接,我终于成功了。 在我的例子中,textBox3.Text 依赖于其他两个文本框,所以我需要的是 MultiBinding 以及相应的实现 IMultiConverter 接口的转换器类。

<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox3" VerticalAlignment="Top" Width="120">
    <TextBox.Text>
          <MultiBinding Converter="{StaticResource changeConverter}">
              <Binding ElementName="textBox2" Path="Text"></Binding>
              <Binding ElementName="textBox3" Path="Text"></Binding>
          </MultiBinding>
    </TextBox.Text>
<TextBox/>

转换器类是

public class ChangeConverter: IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                double par1 = double.Parse(values[0].ToString());
                double par2 = double.Parse(values[1].ToString());
                double res = par2 - par1;
                return res.ToString();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes,
               object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("Cannot convert back");
        }
    }

感谢所有帮助过的人!我希望这个答案将来对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 2013-04-12
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    相关资源
    最近更新 更多