根据另一个NumericUpDown控件更新NumericUpDown值

时间:2014-08-19 09:31:02

标签: c# silverlight xaml windows-phone-8

伙计,如果我的XAML中有2个数字更新,就像这样

<Input:SfNumericUpDown Grid.Column="1" Margin="10,0,10,0"  Value="{Binding CompoundQty}" ValueChanged="SfNumericUpDown_ValueChanged"/>
<Input:SfNumericUpDown Grid.Column="4" Margin="10,0,10,0" Value="{Binding ResultQty}" Tag="{Binding ItemID}" />

我希望根据第一个numericupdown值更改第二个numericupdown值。 我试着将它放在Code中。

private void SfNumericUpDown_ValueChanged(object sender, Syncfusion.UI.Xaml.Controls.Input.ValueChangedEventArgs e)
    {
        var numCompoundQty = (SfNumericUpDown)sender;
        foreach(var row in _entityEdited.ListCompoundDisplay)
        {
            if(row.ItemID == Convert.ToInt32(numCompoundQty.Tag))
            {
                row.CompoundQty = Convert.ToDecimal(numCompoundQty.Value);
                row.ResultQty = Convert.ToDecimal(numDispenseQty.Value) * row.CompoundQty;
                break;
            }
        }



    }

但它不起作用,你们可以帮助我解决问题吗?

2 个答案:

答案 0 :(得分:2)

尝试在两个属性上引发NotifiyPropertyChanged事件。

如果没有,Binding Engine无法检测到值更改,而不会更新您的值。

答案 1 :(得分:2)

您在这里遇到问题,因为您正在尝试引用一个名为&#34; numDispenseQty&#34;的元素,您无法访问该元素(假设它是在DataTemplate中)。而不是处理&#34;值已更改&#34; event,您应该使用对视图模型的双向绑定:

<Input:SfNumericUpDown Value="{Binding CompoundQty,Mode=TwoWay}" />

现在将更新逻辑放入&#34; CompoundQty&#34;的设置器中。财产,例如:

public double CompoundQty
{
    get { return _compoundQty; }
    set
    { 
        _compoundQty = value;
        UpdateResultQty();
        RaisePropertyChanged("CompoundQty");
    }
}

private void UpdateResultQty()
{
    ResultQty = DispenseQty * CompoundQty;
}

请注意(如@sslazio1900注释)您的视图模型类必须实现INotifyPropertyChanged,并在属性更改时引发PropertyChanged事件(这是视图知道更新自身的方式)。 / p>

相关问题