Skip to content
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 Undo/Redo Value-Binding not updated #3420

Closed
TZM-svat opened this issue Jan 10, 2019 · 0 comments
Closed

NumericUpDown Undo/Redo Value-Binding not updated #3420

TZM-svat opened this issue Jan 10, 2019 · 0 comments
Labels
Milestone

Comments

@TZM-svat
Copy link

Description
Using Undo/Redo (Ctrl+Z / Ctrl+Y) works correctly while NumericUpDown has the initial focus. After losing focus once and refocusing the control, Undo/Redo only changes the displayed value but the binded property is never updated. After losing focus again the displayed value returns to the unchanged binded property value.

To Reproduce
MainWindow:

<Window x:Class="WpfMahAppsNumericUpDown.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfMahAppsNumericUpDown"
        xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="30" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <TextBlock Text="NumericUpDown" FontWeight="Bold" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Margin="0,0,0,10"/>

        <TextBlock Text="Value" Grid.Row="1" Grid.Column="0" />
        <mah:NumericUpDown Value="{Binding NumericUpDownValue, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.Column="1" />

        <TextBlock Text="Read only" Grid.Row="2" Grid.Column="0" />
        <mah:NumericUpDown Value="{Binding NumericUpDownValue, UpdateSourceTrigger=PropertyChanged}" Grid.Row="2" Grid.Column="1"
                 IsReadOnly="True" />

        <TextBlock Text="TextBox" FontWeight="Bold" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Margin="0,0,0,10"/>

        <TextBlock Text="Value" Grid.Row="5" Grid.Column="0"/>
        <TextBox Text="{Binding TextBoxValue, UpdateSourceTrigger=PropertyChanged}" Grid.Row="5" Grid.Column="1"/>

        <TextBlock Text="Read only" Grid.Row="6" Grid.Column="0"/>
        <TextBox Text="{Binding TextBoxValue, UpdateSourceTrigger=PropertyChanged}" Grid.Row="6" Grid.Column="1" IsReadOnly="True"/>
    </Grid>
</Window>

ViewModel:

    public class MainWindowViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private double _numericUpDownValue;
        private double _textBoxValue;

        public MainWindowViewModel()
        {
            NumericUpDownValue = 5;
            TextBoxValue = 5;
        }

        public double NumericUpDownValue
        {
            get => _numericUpDownValue;
            set
            {
                if (Math.Abs(_numericUpDownValue - value) < double.Epsilon) return;
                _numericUpDownValue = value;
                OnPropertyChanged(nameof(NumericUpDownValue));
            }
        }
        public double TextBoxValue
        {
            get => _textBoxValue;
            set
            {
                if (Math.Abs(_textBoxValue - value) < double.Epsilon) return;
                _textBoxValue = value;
                OnPropertyChanged(nameof(TextBoxValue));
            }
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Steps to reproduce the behavior:

  1. Change value in NumericUpDown
  2. Click other control to lose focus
  3. Click NumericUpDown to get focus again and press Ctrl+Z (Second control with same Binding is not updated)
  4. Click other control to lose focus again (First control reverts back to binded value)
  5. Do the same with the default TextBox controls -> everything works

Expected behavior
The Undo/Redo commands should always correctly change the binded property like the default TextBox already does.

Screenshots
screenshot

Environment:

  • MahApps.Metro version 1.6.5
  • .NET Framework 4.6.2

Repo
https://github.com/TZM-svat/MahAppsNumericUpDown.git

@punker76 punker76 added the Bug label Jan 18, 2019
@punker76 punker76 added this to the 2.0.0 milestone Jan 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants