简体   繁体   中英

C#/WPF - IntegerUpDown Control PropertyChanged

I'm using an IntegerUpDown from the Extended WPF Toolkit :

<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. 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.

You could handle the Loaded event of the IntegerUpDown control and set the IsReadOnly property of the TextBox to false to "disable the 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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