简体   繁体   English

两个文本框之一未更新绑定

[英]One of two text boxes are not updated with binding

I have two search text boxes, Above and Below , that I have to set restrains on. 我有两个搜索文本框, AboveBelow ,我必须设置限制。 The number in Below can't be higher than the number in Above and the number in Above can't be lower than the one in Below . Below的数字不能高于Above的数字,而Above的数字不能低于Below

If one number is not right, it will should be set equal to the other number. 如果一个数字不正确,则应将其设置为等于另一个数字。

The problem is that Above doesn't get updated, while Bottom does (even though the properties are set in the same way). 问题是Above不会更新,而Bottom会更新(即使属性设置方式相同)。

xaml : xaml

<common:SearchTextBox Grid.Column="1" 
    VerticalAlignment="Center"
    Label="Enter value in feet"
    common:AllowableTextInput.IsIgnoreWhiteSpace="True"
    common:AllowableTextInput.IsMatch="^[0-9]{0,5}$"
    Text="{Binding Path=AboveAircraft, UpdateSourceTrigger=PropertyChanged,
      Converter={StaticResource AboveAircraftConveter}, ConverterParameter=4000}"/>
<Label Grid.Row="1" Grid.Column="0" 
    Style="{StaticResource FormLabelStyle}"
    Content="Below Aircraft (ft):"/>
<common:SearchTextBox Grid.Row="1" Grid.Column="1" 
    VerticalAlignment="Center" HorizontalAlignment="Stretch"
    common:AllowableTextInput.IsIgnoreWhiteSpace="True"
    common:AllowableTextInput.IsMatch="^[0-9]{0,5}$"
    Text="{Binding Path=BelowAircraft, UpdateSourceTrigger=PropertyChanged,
      Converter={StaticResource BelowAircraftConveter}, ConverterParameter=2000}"
    Label="Enter value in feet" />

C# : C#

public int AboveAircraft
{
    get { return _above; }
    set
    {   
        if (SetProperty(ref _above, value, "AboveAircraft") && _updateModel)
        {
            if (Model.AltitudeBand == null)
            {
                Model.AltitudeBand = new AltitudeBand();
            }

            if (PropertyChanged != null)
            {

                PropertyChanged(this, new PropertyChangedEventArgs("AboveAircraft"));
                if (_above < _below)
                {
                     _below = AboveAircraft;
                }
            }

            Model.AltitudeBand.Above = new AltitudeBandLimit() { Unit = AltitudeUnit.Foot, Value = _above };
        }
    }
}

/// <summary>
/// Below the route of flight in ft
/// </summary>
public int BelowAircraft
{
    get { return _below; }
    set
    {
       if (SetProperty(ref _below, value, "BelowAircraft") && _updateModel)
       {
            if (Model.AltitudeBand == null)
            {
                Model.AltitudeBand = new AltitudeBand();
            }

            if (PropertyChanged != null)
            {
                _below = value;
                PropertyChanged(this, new PropertyChangedEventArgs("BelowAircraft"));
                if (_below > _above)
                {
                    AboveAircraft = _below;
                }
            }
           Model.AltitudeBand.Below = new AltitudeBandLimit() { Unit = AltitudeUnit.Foot, Value = _below };
        }
    }
}

You are using _below = AboveAircraft; 您正在使用_below = AboveAircraft; in your AboveAircraft setter method but you are binding BelowAircraft . 在你的AboveAircraft setter方法中你绑定了BelowAircraft

Either change _below = AboveAircraft; 要么改变_below = AboveAircraft; to BelowAircraft = AboveAircraft; 飞往BelowAircraft = AboveAircraft; or send notification for BelowAircraft too, ie 或者也发送BelowAircraft通知,即

            if (PropertyChanged != null)
            {

                PropertyChanged(this, new PropertyChangedEventArgs("AboveAircraft"));
                if (_above < _below)
                {
                     _below = AboveAircraft;
                     PropertyChanged(this, new PropertyChangedEventArgs("BelowAircraft"));
                }
            }

When you change a property that changes another binded property, you have to send notification for both Properties, not only one. 当您更改更改另一个绑定属性的属性时,您必须发送两个属性的通知,而不仅仅是一个。

Of course you have to apply the same changes to the BelowAircraft setter too. 当然,您也必须对BelowAircraft设置器应用相同的更改。

Edit : Just for clarification: Which one of the both methods depends if setting the property would cause a circular event triggering or not. 编辑 :只是为了澄清:两种方法中的哪一种取决于设置属性是否会导致循环事件触发。 If it would cause a circular event (ie endless triggering) then you only have to send a second notification like the code above. 如果它会导致循环事件(即无限触发),那么您只需发送第二个通知,如上面的代码。

Edit 2 : In reply to the comment, your XAML Binding is set to UpdateSourceTrigger=PropertyChanged . 编辑2 :在回复注释时,您的XAML绑定设置为UpdateSourceTrigger=PropertyChanged This means, your setter will be called with each character you type in. 这意味着,您输入的每个字符都会调用您的setter

When you enter "4000" the first character in the "Above" field will be 4 and this causes the below value to 4. Instead you want to update the Text field only when the user has completed his input. 当您输入“4000”时,“Above”字段中的第一个字符将为4,这会导致以下值为4.相反,您只想在用户完成输入时更新“文本”字段。

According to the MSDN documentation for " UpdateSourceTrigger Enumeration " there are 4 values. 根据“ UpdateSourceTrigger Enumeration ”的MSDN文档,有4个值。 For text field you may want to use UpdateSourceTrigger=Default or UpdateSourceTrigger=LostFocus . 对于文本字段,您可能希望使用UpdateSourceTrigger=DefaultUpdateSourceTrigger=LostFocus Then the setter will only be called when the user is finished with his input instead of calling the setter on each character typed in the text field. 然后只有当用户完成输入时才会调用setter,而不是在文本字段中键入的每个字符上调用setter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM