WPF在TextBox中绑定数值以相对于彼此更改 [英] WPF bind numeric values in TextBoxes to change relative to each other

查看:224
本文介绍了WPF在TextBox中绑定数值以相对于彼此更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何绑定多个保存数值的WPF文本框如何更改相同的绝对量? /i.stack.imgur.com/0ouiv.pngalt =多个文本框>



说我有上述UI。如果没有选中复选框,我需要定期绑定到ViewModel属性(这是我现在的):

  TextBox Text ={Binding Value1}/> 
< TextBox Text ={Binding Value2}/>
< TextBox Text ={Binding Value3}/>
< TextBox Text ={Binding Value4}/>
< TextBox Text ={Binding Value5}/>
< CheckBox Content =相对变化常量IsChecked ={Binding UseRelative}/>

如果检查了CheckBox,并且任何TextBox中的值都已更改,我需要拥有所有其他TextBox值更新相同的相对数量向上或向下。
例如,如果第二个TextBox值从25 - > 30更新。其他4个TextBox值应该变为39,26,30和32(增加5)。

解决方案

这是一个仅使用两个属性的解决方案。

  private int _value1; 
public int Value1
{
[DebuggerStepThrough]
get {return _value1;
[DebuggerStepThrough]
set
{
if(value!= _value1)
{
if(UseRelative)
{
UpdateRelative(value - _value1);
}
else
{
_value1 = value;
OnPropertyChanged(Value1);
}
}
}
}
private int _value2;
public int Value2
{
[DebuggerStepThrough]
get {return _value2;
[DebuggerStepThrough]
set
{
if(value!= _value2)
{
if(UseRelative)
{
UpdateRelative(value - _value2)
}
else
{
_value2 = value;
OnPropertyChanged(Value2);
}
}
}
}
private void UpdateRelative(int increment)
{
_value1 + = increment;
_value2 + = increment;
//更新视图
OnPropertyChanged(Value1);
OnPropertyChanged(Value2);
}

在这个例子中,在支持字段之前干预了setter如果UseRelative值为true,则设置哪个查询。如果是,则setter调用UpdateRelative方法并返回。



UpdateRelative方法在后备字段上运行,一旦执行计算,它在 public 字段上调用OnPropertyChanged。 WPF绑定引擎将查询getter并更新视图(因为getter使用后缀字段)。



当UseRelative为false时,它是照常的业务:即标准的WPF属性模型。此示例中的两个属性可以扩展为使用相同的技术将5包含在您的问题中。


How to bind multiple WPF TextBoxes holding numerical values to change the same absolute amount if one of the TextBox value is changed?

Lets say I have the above UI. If the CheckBox is not checked, I need the regular binding to ViewModel property (this is what I have now):

<TextBox Text="{Binding Value1}" />
<TextBox Text="{Binding Value2}" />
<TextBox Text="{Binding Value3}" />
<TextBox Text="{Binding Value4}" />
<TextBox Text="{Binding Value5}" />
<CheckBox Content="Relative Changes Constant" IsChecked="{Binding UseRelative}" />

If the CheckBox is checked however and the value in any TextBox is changed, I need to have all other TextBox values updated the same relative amount up or down. For example, if the 2nd TextBox value is updated from 25 -> 30. The other 4 TextBox values should then become 39, 26, 30 and 32 (increase by 5).

解决方案

Here's a solution that uses just two properties...

  private int _value1;
    public int Value1
    {
        [DebuggerStepThrough]
        get { return _value1; }
        [DebuggerStepThrough]
        set
        {
            if (value != _value1)
            {
                if (UseRelative)
                {
                    UpdateRelative(value - _value1);
                }
                else
                {
                    _value1 = value;
                    OnPropertyChanged("Value1");
                }
            }
        }
    }
    private int _value2;
    public int Value2
    {
        [DebuggerStepThrough]
        get { return _value2; }
        [DebuggerStepThrough]
        set
        {
            if (value != _value2)
            {
                if (UseRelative)
                {
                    UpdateRelative(value - _value2)
                }
                else
                {
                    _value2 = value;
                    OnPropertyChanged("Value2");
                }
            }
        }
    }
    private void UpdateRelative(int increment)
    {
        _value1 += increment;
        _value2 += increment;
        // update the view
        OnPropertyChanged("Value1");
        OnPropertyChanged("Value2");
    }

In this example, there is an intervention in the setter just before the backing field is set which queries if the UseRelative value is true. If it is, the setter calls the UpdateRelative method and returns.

The UpdateRelative method operates on the backing fields and once the calculations are performed, it calls OnPropertyChanged on the public fields. The WPF binding engine will then query the getters and update the view (because the getters use the backing fields).

When UseRelative is false, it's business as usual: i.e., the standard WPF property model. The two properties in this sample can be extended to include the 5 in your question using the same technique.

这篇关于WPF在TextBox中绑定数值以相对于彼此更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆