Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Funkest committed Feb 29, 2024
1 parent a54ece2 commit a1c5292
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
8 changes: 5 additions & 3 deletions sandbox/WpfApp1/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
<Label Content="Input" />
<TextBox Text="{Binding Input.Value, UpdateSourceTrigger=PropertyChanged}" />
<Label Content="BindableReactiveProperty.Value" />
<TextBlock Text="{Binding Output.Value}" />
<TextBlock Text="{Binding Output.Value}" Margin="40 0" />
<Label Content="ReadOnlyBindableReactiveProperty.CurrentValue" />
<TextBlock Text="{Binding OutputRO.CurrentValue}" />
<TextBlock Text="{Binding OutputRO2.CurrentValue}" />
<Label Content="from BindableReactiveProperty" Margin="20 0" />
<TextBlock Text="{Binding OutputRO.CurrentValue}" Margin="40 0" />
<Label Content="from ReadOnlyReactiveProperty" Margin="20 0" />
<TextBlock Text="{Binding OutputRO2.CurrentValue}" Margin="40 0" />
<Button Content="add" Command="{Binding AddCommand}" />
</StackPanel>

Expand Down
40 changes: 28 additions & 12 deletions src/R3/BindableReactiveProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,12 @@ protected override void OnValueChanged(T value)

if (!validationContext.TryValidateValue(value, errors))
{
ErrorsChanged?.Invoke(this, ValueChangedEventArgs.GetDataErrorsChangedEventArgs(isReadOnly));

// need both, even if read-only; because read-only instance by this.ToReadOnlyBindableReactiveProperty() is just a cast with side-effect
ErrorsChanged?.Invoke(this, ValueChangedEventArgs.DataErrorsChanged);
if (isReadOnly)
{
ErrorsChanged?.Invoke(this, ValueChangedEventArgs.DataErrorsChangedReadOnly);
}
// set is completed(validation does not call before set) so continue call PropertyChanged
}
}
Expand All @@ -163,12 +167,22 @@ protected override void OnValueChanged(T value)
if (errors != null && errors.Count != 0)
{
errors.Clear();
ErrorsChanged?.Invoke(this, ValueChangedEventArgs.GetDataErrorsChangedEventArgs(isReadOnly));
// need both, even if read-only; because read-only instance by this.ToReadOnlyBindableReactiveProperty() is just a cast with side-effect
ErrorsChanged?.Invoke(this, ValueChangedEventArgs.DataErrorsChanged);
if (isReadOnly)
{
ErrorsChanged?.Invoke(this, ValueChangedEventArgs.DataErrorsChangedReadOnly);
}
}
}
}

PropertyChanged?.Invoke(this, ValueChangedEventArgs.GetPropertyChangedEventArgs(isReadOnly));
// need both, even if read-only; because read-only instance by this.ToReadOnlyBindableReactiveProperty() is just a cast with side-effect
PropertyChanged?.Invoke(this, ValueChangedEventArgs.PropertyChanged);
if (isReadOnly)
{
PropertyChanged?.Invoke(this, ValueChangedEventArgs.PropertyChangedReadOnly);
}
}

// for INotifyDataErrorInfo
Expand Down Expand Up @@ -226,7 +240,12 @@ protected override void OnReceiveError(Exception exception)
errors.Add(new ValidationResult(exception.Message));
}

ErrorsChanged?.Invoke(this, ValueChangedEventArgs.GetDataErrorsChangedEventArgs(isReadOnly));
// need both, even if read-only; because read-only instance by this.ToReadOnlyBindableReactiveProperty() is just a cast with side-effect
ErrorsChanged?.Invoke(this, ValueChangedEventArgs.DataErrorsChanged);
if (isReadOnly)
{
ErrorsChanged?.Invoke(this, ValueChangedEventArgs.DataErrorsChangedReadOnly);
}
}

public BindableReactiveProperty<T> EnableValidation()
Expand Down Expand Up @@ -315,11 +334,8 @@ public bool TryValidateValue(object? value, ICollection<ValidationResult> valida

internal static class ValueChangedEventArgs
{
static readonly PropertyChangedEventArgs propertyChanged = new PropertyChangedEventArgs("Value");
static readonly PropertyChangedEventArgs propertyChangedReadOnly = new PropertyChangedEventArgs("CurrentValue");
static readonly DataErrorsChangedEventArgs dataErrorsChanged = new DataErrorsChangedEventArgs("Value");
static readonly DataErrorsChangedEventArgs dataErrorsChangedReadOnly = new DataErrorsChangedEventArgs("CurrentValue");

internal static PropertyChangedEventArgs GetPropertyChangedEventArgs(bool isReadOnly) => isReadOnly ? propertyChangedReadOnly : propertyChanged;
internal static DataErrorsChangedEventArgs GetDataErrorsChangedEventArgs(bool isReadOnly) => isReadOnly ? dataErrorsChangedReadOnly : dataErrorsChanged;
internal static readonly PropertyChangedEventArgs PropertyChanged = new PropertyChangedEventArgs("Value");
internal static readonly PropertyChangedEventArgs PropertyChangedReadOnly = new PropertyChangedEventArgs("CurrentValue");
internal static readonly DataErrorsChangedEventArgs DataErrorsChanged = new DataErrorsChangedEventArgs("Value");
internal static readonly DataErrorsChangedEventArgs DataErrorsChangedReadOnly = new DataErrorsChangedEventArgs("CurrentValue");
}

0 comments on commit a1c5292

Please sign in to comment.