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

Property IsEnabled is not propagated to classes inherited from UserControl #18350

Open
eSqadron opened this issue Mar 1, 2025 · 1 comment
Open

Comments

@eSqadron
Copy link

eSqadron commented Mar 1, 2025

Describe the bug

Property IsEnabled is not propagated to classes inheriting UserControl

I have custom UserControl, and I try to intercept IsEnabledProperty change inside of a class that inherits UserControl.

this.GetObservable(IsEnabledProperty).Subscribe(isEnabled => 
{
    // log, and debugger set in here
});

Above subscriber is never called, even when I properly bind IsEnabled in XAML.

To Reproduce

  1. Add:
    [ObservableProperty]
    private bool _isCustomUserControl = true;

To MainViewModel.cs
2. With:

     this.PropertyChanged += (sender, args) =>
     {
         if (args.PropertyName == nameof(IsCustomUserControlEnabled))
         {
             // log, and debugger breakpoint set in here
         }
     };

Added to constructor of MainViewModel, I can see that IsCustomUserControlEnabled is actually updated
3. Add:
<local:CustomUserControl IsEnabled="{Binding IsCustomUserControlEnabled/>
To MainWindow.xaml
4. Create public partial class CustomUserControl : UserControl
5. With:

        this.GetObservable(IsEnabledProperty).Subscribe(isEnabled => 
        {
            // log, and debugger breakpoint set in here
        });

Added to constructor CustomUserControl.xaml.cs, I can see that IsCustomUserControlEnabled is not propagated to IsEnabled of CustomUserControl

If this is too confusing to reproduce I can provide you with sample app, but I think it is straight forward enough

Expected behavior

Step 5 - Properties inherited from UserControl, bound using MVVM ObservableProperty, should be propagated to child classes.
Which means, that when I update IsCustomUserControlEnabled, breakpoint inside of subscriber to IsEnabledProperty should be hit

Avalonia version

11.2.1

OS

Windows

Additional context

No response

@eSqadron eSqadron added the bug label Mar 1, 2025
@timunie
Copy link
Contributor

timunie commented Mar 1, 2025

UserControl is probably the wrong control type for you. See it as a page, not a control like a custom button. Consider to inherit ContentControl or TemplatedControl for example.

https://github.com/AvaloniaUI/Avalonia.Samples/tree/main/src/Avalonia.Samples/CustomControls/RatingControlSample

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

No branches or pull requests

2 participants