New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NumericUpDown Value not displayed #2515

Closed
puchs opened this Issue May 20, 2016 · 5 comments

Comments

Projects
None yet
6 participants
@puchs

puchs commented May 20, 2016

I know it sounds weird, but the NumericUpDown control will not show a value if:

1.) it is part of a DataTemplate
2.) and it contains a converter in a Binding of the Value property.
3.) and if either the Maximum or Minimum properties are set.

I have created a small sample solution, that exhibits the problem:

NumericUpDownTest.zip

The main window contains a ContentControl binding to the property "V" of the code-behind of the MainWindow class. It also contains a DataTemplate with two NumericUpDownControls. They are almost identical:

<NumericUpDown Grid.Row="0" Grid.Column="0" Height="23" Margin="5,5,5,5" Interval="1" StringFormat="{}{0:###0}" Value="{Binding Path=V, Converter={StaticResource identityConverter}, UpdateSourceTrigger=LostFocus}" />

The only difference is that one of the controls also contains the following two attributes (one is sufficient to trigger the problem):

Maximum="2000" Minimum="-273"

This control will not display the value bound to the NumericUpDown. The two attributes

diag:PresentationTraceSources.TraceLevel=High, Mode=TwoWay,

of the Binding do not change the situation and neither does the StringFormat attribute. The BindingExpression seems to "vanish" - at least that is what I see in the WPF debugger of Visual Studio 2015. I can also see (thanks to the Binding diagnostics) that the converter is called and returns a correct value.

How did I find the problem: I exchanged all occurences of the DoubleUpDown from the Extended WPF Toolkit in my application with the MahApps.Metro NumericUpDown. The problem appeared only after this exchange.

Expected outcome

Both NumericUpDownControls should display the same value.

Environment

  • MahApps.Metro 1.2.4, but I also could reproduce the problem with the development branch @ commit: 1c6480d
  • Windows 7 x64 all patches installed
  • Visual Studio 2015 Community
  • .NET Framework 4.5.2 (and also 4.6.1)

@punker76 punker76 added the Bug label May 20, 2016

@punker76 punker76 self-assigned this May 20, 2016

@barjonas

This comment has been minimized.

Show comment
Hide comment
@barjonas

barjonas Jun 16, 2016

Even though it sounds obscure, I just stumbled this same bug with exactly the same 3 criteria met. Everything works if I remove either the converter or the min and max properties. Otherwise I can see that it Convert is called on the converter, but no value is displayed. When value is set through the control's UI, ConvertBack is never called.

barjonas commented Jun 16, 2016

Even though it sounds obscure, I just stumbled this same bug with exactly the same 3 criteria met. Everything works if I remove either the converter or the min and max properties. Otherwise I can see that it Convert is called on the converter, but no value is displayed. When value is set through the control's UI, ConvertBack is never called.

@bradley-dotnet

This comment has been minimized.

Show comment
Hide comment
@bradley-dotnet

bradley-dotnet Jan 17, 2017

This issue is still present, and seems to occur even without the converter; just a bound value in a data template breaks it.

bradley-dotnet commented Jan 17, 2017

This issue is still present, and seems to occur even without the converter; just a bound value in a data template breaks it.

@sbrett9

This comment has been minimized.

Show comment
Hide comment
@sbrett9

sbrett9 Jan 23, 2017

For me this seem to occur if the following are true.

A) The NumericUpDown is part of a DataTemplate
B) The Value is bound to a property of the data context of the DataTemplate
C) Maximum and/or Minimum values have been set in the xaml.

After looking through the source for 1.30 (as that is what I am currently dedicated to), i found that there seems to be issues with the order the Max, Min and Value properties are read in on load.
If the read order is Value, and then either Max or Min, things seem to work fine.
If the min or max is read prior to the value, the binding expression bound to Value up and disappears and the control's DependencyProperty named ValueProperty remains null.

As I needed all 3, I figured out a work around.

Create a style for the NumericUpDown that checks the control's load flag and if it is loaded, set the max and min to the desired values.

The following is my full data template.

        <DataTemplate DataType="{x:Type volume:UniformVolumeData}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0"
                           HorizontalAlignment="Left"
                           Text="Volume: " />
                <controls1:NumericUpDown x:Name="volumeUpDown"
                                         Grid.Column="1"
                                         MaxWidth="200"
                                         controls1:TextBoxHelper.ClearTextButton="True" controls1:TextBoxHelper.SelectAllOnFocus="True" controls1:TextBoxHelper.Watermark="Enter Volume"
                                         Focusable="True" HasDecimals="True" StringFormat=" {0:N2} μL"
                                         Value="{Binding Path=DataContext.Volume, ElementName=volumeUpDown, Converter={StaticResource ConstantExpressionToNumberConverter}, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, PresentationTraceSources.TraceLevel=High}">
                    <controls1:NumericUpDown.Style>
                        <Style TargetType="{x:Type controls1:NumericUpDown}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=IsLoaded, ElementName=volumeUpDown}" Value="true">
                                    <Setter Property="Maximum" Value="1000000" />
                                    <Setter Property="Minimum" Value="0" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </controls1:NumericUpDown.Style>
                </controls1:NumericUpDown>
            </Grid>
        </DataTemplate>

sbrett9 commented Jan 23, 2017

For me this seem to occur if the following are true.

A) The NumericUpDown is part of a DataTemplate
B) The Value is bound to a property of the data context of the DataTemplate
C) Maximum and/or Minimum values have been set in the xaml.

After looking through the source for 1.30 (as that is what I am currently dedicated to), i found that there seems to be issues with the order the Max, Min and Value properties are read in on load.
If the read order is Value, and then either Max or Min, things seem to work fine.
If the min or max is read prior to the value, the binding expression bound to Value up and disappears and the control's DependencyProperty named ValueProperty remains null.

As I needed all 3, I figured out a work around.

Create a style for the NumericUpDown that checks the control's load flag and if it is loaded, set the max and min to the desired values.

The following is my full data template.

        <DataTemplate DataType="{x:Type volume:UniformVolumeData}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0"
                           HorizontalAlignment="Left"
                           Text="Volume: " />
                <controls1:NumericUpDown x:Name="volumeUpDown"
                                         Grid.Column="1"
                                         MaxWidth="200"
                                         controls1:TextBoxHelper.ClearTextButton="True" controls1:TextBoxHelper.SelectAllOnFocus="True" controls1:TextBoxHelper.Watermark="Enter Volume"
                                         Focusable="True" HasDecimals="True" StringFormat=" {0:N2} μL"
                                         Value="{Binding Path=DataContext.Volume, ElementName=volumeUpDown, Converter={StaticResource ConstantExpressionToNumberConverter}, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, PresentationTraceSources.TraceLevel=High}">
                    <controls1:NumericUpDown.Style>
                        <Style TargetType="{x:Type controls1:NumericUpDown}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=IsLoaded, ElementName=volumeUpDown}" Value="true">
                                    <Setter Property="Maximum" Value="1000000" />
                                    <Setter Property="Minimum" Value="0" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </controls1:NumericUpDown.Style>
                </controls1:NumericUpDown>
            </Grid>
        </DataTemplate>
@iXab3r

This comment has been minimized.

Show comment
Hide comment
@iXab3r

iXab3r Mar 24, 2017

There is some bizarre behavior regarding conversion. I was using version 1.1.2.0 and yesterday I've decided to update to 1.4.3. As a result all my NumericUpDown usages broke - changes were not propagated from controls to viewmodels.

I've tried to find the reason, but could only find a "kludge" that partially helped.
I have used same approach with Converter="{StaticResource ConverterKey}" for my Value binding and it appears that replacing StaticResource usage with in-place instantiation of Converter has helped. I.e. instead of referencing converter by Key you should either reference it as a static field

  • Converter={x:Static Converters.MyFavoriteConverter}
    or create a new one

  • <Converter.Value> < MyFavoriteConverter /> </Converter.Value>.

iXab3r commented Mar 24, 2017

There is some bizarre behavior regarding conversion. I was using version 1.1.2.0 and yesterday I've decided to update to 1.4.3. As a result all my NumericUpDown usages broke - changes were not propagated from controls to viewmodels.

I've tried to find the reason, but could only find a "kludge" that partially helped.
I have used same approach with Converter="{StaticResource ConverterKey}" for my Value binding and it appears that replacing StaticResource usage with in-place instantiation of Converter has helped. I.e. instead of referencing converter by Key you should either reference it as a static field

  • Converter={x:Static Converters.MyFavoriteConverter}
    or create a new one

  • <Converter.Value> < MyFavoriteConverter /> </Converter.Value>.

@punker76 punker76 added this to the 1.5.0 milestone Apr 13, 2017

@punker76 punker76 closed this in 709cf89 Apr 13, 2017

@punker76

This comment has been minimized.

Show comment
Hide comment
@punker76

punker76 Apr 13, 2017

Member

@puchs Thx for the test app. This issue will be fixed and available in 1.5.0

Member

punker76 commented Apr 13, 2017

@puchs Thx for the test app. This issue will be fixed and available in 1.5.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment