简体   繁体   English

C#/ WPF-IntegerUpDown控件的PropertyChanged

[英]C#/WPF - IntegerUpDown Control PropertyChanged

I'm using an IntegerUpDown from the Extended WPF Toolkit : 我正在使用Extended WPF Toolkit中IntegerUpDown

<xctk:IntegerUpDown Value="{Binding ProposedGrade, Mode=TwoWay}" Name="gradeBox" Margin="118,10,32,0" FormatString="N0" DefaultValue="1" Increment="1" Minimum="1" Maximum="5" Grid.Row="1" Grid.Column="1"/>
<Button Content="Approve grade" IsEnabled="{Binding EnableGradeApproval}" Command="{Binding SaveGradeCommand }" Margin="50,66,172,-56" Grid.Row="1" />

My issue is, that even though the min and max values can be set, I am able to update the value beyond that range by using the numerical keyboard. 我的问题是,即使可以设置最小值和最大值,我也可以使用数字键盘来更新超出该范围的值。 Also, if any integer outside of the allowed range is entered, the property change event doesn't fire, so I'm not able to validate the input whenever a user decides to enter numbers from the keyboard - and thus my button stays enabled even for large numbers. 另外,如果输入了超出允许范围的整数,则不会触发属性更改事件,因此,每当用户决定从键盘输入数字时,我都无法验证输入-因此,即使我的按钮也保持启用状态为大量。 How can I solve this? 我该如何解决? Is there a way to either fire the property changed event, or disable the keyboard? 有没有办法触发属性更改事件或禁用键盘?

So what happens is, that this: 那么发生的是:

public Int32 ProposedGrade
{
    get { return _proposedGrade; }
    private set
    {
        if (_proposedGrade != value)
        {
            _proposedGrade = value;
            if (_proposedGrade > 0 && _proposedGrade < 6)
            {
                EnableGradeApproval = true;
                OnPropertyChanged("EnableGradeApproval");
            }
            else
            {
                EnableGradeApproval = false;
                OnPropertyChanged("EnableGradeApproval");

            }
            OnPropertyChanged("ProposedGrade");
        }
    }
}

Doesn't get called if I enter 7 for example from the keyboard. 例如,如果我从键盘输入7,不会被呼叫。 If I enter 4, it does get called, buth then I don't need to disable the grade approval, so not much use in that. 如果输入4,它确实会被调用,但是我不需要禁用等级批准,因此在其中没有太多用处。

You could handle the Loaded event of the IntegerUpDown control and set the IsReadOnly property of the TextBox to false to "disable the TextBox ": 您可以处理IntegerUpDown控件的Loaded事件,并将TextBoxIsReadOnly属性设置为false以“禁用TextBox ”:

<xctk:IntegerUpDown Value="{Binding ProposedGrade, Mode=TwoWay}" Name="gradeBox" Margin="118,10,32,0" FormatString="N0" 
                    DefaultValue="1" Increment="1" Minimum="1" Maximum="5" Grid.Row="1" Grid.Column="1"
                    Loaded="gradeBox_Loaded"/>

private void gradeBox_Loaded(object sender, RoutedEventArgs e)
{
    var textBox = gradeBox.Template.FindName("PART_TextBox", gradeBox) as Xceed.Wpf.Toolkit.WatermarkTextBox;
    if (textBox != null)
    {
        textBox.IsReadOnly = true;
    }
}

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

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