GuOrg / Gu.Wpf.NumericInput

MIT License
74 stars 20 forks source link

Changing StringFormat at runtime destroys the value #35

Closed ora8 closed 4 years ago

ora8 commented 4 years ago

This does not work

                <numeric:DoubleBox
                    x:Name="TextBoxPi2"
                    Grid.Row="1"
                    Grid.Column="0"
                    Width="60"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    behaviours:SelectTextOnFocus.Active="True"
                    IsEnabled="{Binding Path=IsPivFieldsEnabled}"
                    KeyboardNavigation.TabIndex="9"
                    LostFocus="TextBoxPi2_OnLostFocus"
                    StringFormat="{Binding PivStringFormat, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                    TextWrapping="Wrap"
                    Value="{Binding Path=SelectedVariant.Pi2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

   public void UpdatePixStringFormat()
    {
        if (SelectedVariant == null)
            return;

        PivStringFormat = SelectedVariant.M == 5 ? "N4" : "N2";
    }

The value of TextBoxPi2 will be destroyed. Is there a workarround?

Thanks

JohanLarsson commented 4 years ago

Thanks for reporting! Will write UI-tests for it. Sorry about the bug.

JohanLarsson commented 4 years ago

What do you mean by destroys the value? I found existing UI-tests for this

[Test]
public static void WhenStringFormatChanges()
{
    using var app = Application.AttachOrLaunch(ExeFileName, WindowName);
    var window = app.MainWindow;
    var doubleBox = window.FindTextBox("LostFocusValidateOnPropertyChangedBox");
    window.FindTextBox("StringFormat").Text = "F1";

    doubleBox.Text = "1.23456";
    window.FindButton("lose focus").Click();

    Assert.AreEqual(false, doubleBox.HasValidationError());
    Assert.AreEqual("1.23456", doubleBox.Text);
    Assert.AreEqual("1.2", doubleBox.FormattedView().Text);
    Assert.AreEqual("1.23456", window.FindTextBox("ViewModelValue").Text);
    Assert.AreEqual(TextSource.UserInput, doubleBox.TextSource());

    window.FindTextBox("StringFormat").Text = "F4";
    window.FindButton("lose focus").Click();
    Assert.AreEqual(false, doubleBox.HasValidationError());
    Assert.AreEqual("1.23456", doubleBox.Text);
    Assert.AreEqual("1.2346", doubleBox.FormattedView().Text);
    Assert.AreEqual("1.23456", window.FindTextBox("ViewModelValue").Text);
    Assert.AreEqual(TextSource.UserInput, doubleBox.TextSource());

    window.FindTextBox("StringFormat").Text = "F1";
    window.FindButton("lose focus").Click();
    Assert.AreEqual(false, doubleBox.HasValidationError());
    Assert.AreEqual("1.23456", doubleBox.Text);
    Assert.AreEqual("1.2", doubleBox.FormattedView().Text);
    Assert.AreEqual("1.23456", window.FindTextBox("ViewModelValue").Text);
    Assert.AreEqual(TextSource.UserInput, doubleBox.TextSource());
}
ora8 commented 4 years ago

Thank You very much for testing. It is not issue. There was a problem with using a SelectionChanged event of Combo Box. The value was always set to dirty. Sorry for the wrong alert.

JohanLarsson commented 4 years ago

No problem, keep the issues coming! I'm closing this issue.