WPF 怎么实现前两个文本框的乘积为第三个文本框的内容
WPF 如何实现前两个文本框的乘积为第三个文本框的内容!
我需要的是动态刷新第三个文本框,就是前两个文本框的内容改变,第三个文本框的内容也会随之改变! 可以用到绑定的方法吗?
textchange这个方法有人提出来了!所以请求别的思路!
------解决思路----------------------
我需要的是动态刷新第三个文本框,就是前两个文本框的内容改变,第三个文本框的内容也会随之改变! 可以用到绑定的方法吗?
textchange这个方法有人提出来了!所以请求别的思路!
------解决思路----------------------
namespace WpfApplication1
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public class MULConverter : IMultiValueConverter
{
public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!string.IsNullOrEmpty(value[0].ToString()) && !string.IsNullOrEmpty(value[1].ToString()))
return (System.Convert.ToInt32(value[0]) * System.Convert.ToInt32(value[1])).ToString();
else
return "null";
//这边的逻辑你自己实现,对象是object,需要很多验证。
}
public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public Window1()
{
InitializeComponent();
Binding binding1 = new Binding();
binding1.Path = new PropertyPath("Text");
binding1.Source = textBox1;
Binding binding2 = new Binding();
binding2.Path = new PropertyPath("Text");
binding2.Source = textBox2;
MultiBinding mbinding = new MultiBinding() { Mode = BindingMode.OneWay };
mbinding.Bindings.Add(binding1);
mbinding.Bindings.Add(binding2);
mbinding.Converter = new MULConverter();
BindingOperations.SetBinding(this.textBox3, TextBox.TextProperty, mbinding);//多个绑定
//BindingOperations.SetBinding(this.textBox3,TextBox.TextProperty,new Binding(){Path = new PropertyPath("Text"), Source = textBox2}); 单个绑定
}
}
}