0

I'm using an IntegerUpDownfrom 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.

lte__
  • 7,175
  • 25
  • 74
  • 131
  • Did you solve your DataGrid issues? – mm8 May 17 '17 at 12:25
  • Honestly, I didn't quite understand the Proxy-object solution, so I just went and worked around it with an extra popup window and some validation... Will get back to it later next week. – lte__ May 17 '17 at 12:29
  • Remove the Maximum attribute, and validate it in Property setter manually. If value > your maximum, just don't assign it to the field, and don't call OnPropertyChanged(). – Yevgeniy May 17 '17 at 12:41
  • @Yevgeniy well that's the issue! If I put in invalid data, the setter doesn't get called at all! If I enter 256 for example, the setter gets called for 2, then doesn't get called for 5 and 6, so 2 stays the `_proposedGrade` value, which is clearly not user-friendly. – lte__ May 17 '17 at 12:51
  • Doesn't setter get called if you remove Maximum attribute from IntegerUpDown? – Yevgeniy May 17 '17 at 12:54
  • @YevgeniyWell it does, but then I'm visually allowing the user to enter incorrect input, since the up arrow never gets greyed out... – lte__ May 17 '17 at 12:56
  • What about disabling the TextBox as I suggested? – mm8 May 17 '17 at 13:01
  • @mm8 There is no textbox. – lte__ May 17 '17 at 13:07
  • Did you really handle the Loaded event as I suggested? There is obviously no TextBox until the control has been loaded. – mm8 May 17 '17 at 13:08
  • I didn't do anything as you suggested, but I'll accept your answer bc you're clearly sure it works so it must be correct. – lte__ May 17 '17 at 13:26
  • You "didn't do anything as I suggested" but yet you know that "there is no TextBox". This makes no sense. What's the point of asking a question if you don't try out the answer you are getting? – mm8 May 17 '17 at 13:31
  • I meant there is no TextBox in my code. I accepted your answer, why are you still so salty? Let me use SO as I wish, I don't tell you how to do stuff either. – lte__ May 17 '17 at 13:33
  • I am just trying to help you. Why didn't you upvote the answer if it solved your issue? https://stackoverflow.com/help/privileges/vote-up – mm8 May 17 '17 at 13:40

1 Answers1

-1

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;
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • According to the documentation, `Loaded` >Occurs when the element is laid out, rendered, and ready for interaction. So I'm not sure how that would help me with user interaction/value updates on the `IntegerUpDown`. – lte__ May 17 '17 at 12:39
  • Well, try the code and you will see that it works...Handling the Loaded event as per above clearly works. You might want to consider voting helpful answers up: https://stackoverflow.com/help/privileges/vote-up – mm8 May 17 '17 at 12:41