Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8781355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:16:49+00:00 2026-06-13T20:16:49+00:00

Consider using inheritance with ReactiveUI. I have base ViewModel class with DoSomethingCommand. ‘CanExecute’ for

  • 0

Consider using inheritance with ReactiveUI.
I have base ViewModel class with DoSomethingCommand. ‘CanExecute’ for this command depends on property Prop1

public class A : ReactiveObject
{
    public int Prop1 { get {...} set {...} }
    public ReactiveCommand DoSomethingCommand { get; private set; }

    public A()
    {
        IObservable<bool> canDoSomething = this.WhenAny(vm => vm.Prop1, p1 => CanDoSomething());
        DoSomethingCommand = new ReactiveCommand(canDoSomething);
        DoSomethingCommand.Subscribe(x => DoSomething());
    }

    protected virtual bool CanDoSomething()
    {
        return ...
    }
}

In inherited class the ‘CanExecute’ for this command depends additionally on property Prop2

public class B : A
{
    public int Prop2 { get {...} set {...} }

    public B()
    {
        //Senseless code. For explanation only
        IObservable<bool> canDeleteExecute = this.WhenAny(vm => vm.Prop1, vm => vm.Prop2, (p1, p2) => CanDoSomething());
    }
}

What is the best practice to create command and make ‘CanExecute’ dependent on properties from base and inherited classes?
Of course, I want inherited classes shouldn’t change when ‘CanExecute’ in base class become additionally depend on AnotherProp property.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:16:51+00:00Added an answer on June 13, 2026 at 8:16 pm

    I’ve written extension class for WhenAny. It’s much more to do for making it like WhenAny in ReactiveUI, but enough for me now. First, look at usage:

    public class A : ReactiveObject
    {
        public A()
        {
            //Using almost like WhenAny from ReactiveUI
            CanExecuteObservable = this.WhenAny(() => AProp, CanExecute);
            Command = new ReactiveCommand(CanExecuteObservable);
            Command.Subscribe(x => Execute());
        }
    
        protected CanExecuteObservable CanExecuteObservable { get; private set; }
        public ReactiveCommand Command { get; private set; }
    
        protected virtual bool CanExecute()
        {
            return AProp > 10;
        }
    
        private int aProp = 10;
        public int AProp { get { return aProp; } set { this.RaiseAndSetIfChanged(x => x.AProp, value); } }
    }
    
    public class B : A
    {
        public B()
        {
            //Add one more property dependency for CanExecute
            CanExecuteObservable.AddProperties(() => BProp);
        }
    
        private int bProp = 10;
        public int BProp { get { return bProp; } set { this.RaiseAndSetIfChanged(x => x.BProp, value); } }
    
        protected override bool CanExecute()
        {
            return base.CanExecute() && BProp > 100;
        }
    }
    

    Implementation:

    public static class WhenAnyExtensions
    {
        public static CanExecuteObservable WhenAny(this IReactiveNotifyPropertyChanged obj,
            IEnumerable<Expression<Func<object>>> expressions, Func<bool> func)
        {
            return new CanExecuteObservable(obj, expressions, func);
        }
    
        public static CanExecuteObservable WhenAny(this IReactiveNotifyPropertyChanged obj, Expression<Func<object>> property1, Func<bool> func)
        {
            return obj.WhenAny(new[] { property1 }, func);
        }
    
        public static CanExecuteObservable WhenAny(this IReactiveNotifyPropertyChanged obj, Expression<Func<object>> property1, Expression<Func<object>> property2, Func<bool> func)
        {
            return obj.WhenAny(new[] { property1, property2 }, func);
        }
    
        //etc...
    }
    
    public class CanExecuteObservable : IObservable<bool>
    {
        internal CanExecuteObservable(IReactiveNotifyPropertyChanged obj,
            IEnumerable<Expression<Func<object>>> expressions, Func<bool> func)
        {
            this.func = func;
            AddProperties(expressions);
            obj
                .Changed
                .Where(oc => propertyNames.Any(propertyName => propertyName == oc.PropertyName))
                .Subscribe(oc => Fire());
        }
    
        private readonly List<string> propertyNames = new List<string>();
        private readonly Func<bool> func;
    
        public void AddProperties(IEnumerable<Expression<Func<object>>> expressions)
        {
            foreach (var expression in expressions)
            {
                string propertyName = ReflectionHelper.GetPropertyNameFromExpression(expression);
                propertyNames.Add(propertyName);
            }
        }
    
        public void AddProperties(Expression<Func<object>> property1) { AddProperties(new[] { property1 }); }
        public void AddProperties(Expression<Func<object>> property1, Expression<Func<object>> property2) { AddProperties(new[] { property1, property2 }); }
        //etc...
    
        public void Clear()
        {
            propertyNames.Clear();
        }
    
        private readonly Subject<bool> subject = new Subject<bool>();
    
        private void Fire()
        {
            subject.OnNext(func());
        }
    
        public IDisposable Subscribe(IObserver<bool> observer)
        {
            return subject.Subscribe(observer);
        }
    }
    

    And uninteresting, in this context, helper class for getting property name from expression:

    public class ReflectionHelper
    {
        public static string GetPropertyNameFromExpression<T>(Expression<Func<T>> property) 
        {
            var lambda = (LambdaExpression)property;
            MemberExpression memberExpression;
    
            if (lambda.Body is UnaryExpression) 
            {
                var unaryExpression = (UnaryExpression)lambda.Body;
                memberExpression = (MemberExpression)unaryExpression.Operand;
            } 
            else 
            {
                memberExpression = (MemberExpression)lambda.Body;
            }
            return memberExpression.Member.Name;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand base constructor implementation. Consider this situation If I have base
Consider the following code: using System; namespace ConsoleApplication2 { class Program { static void
Consider the following inheritance example: class A {...} class B extends A { ..
When I have top-level tree items, should i consider using blank=True , null=True or
THIS QUESTION REFERS TO RESTKIT 0.9.x. IF YOU ARE NEW TO RESTKIT CONSIDER USING
This page suggests !ENTITY: If you want to avoid duplication, consider using XML entities
Would you consider using an interface and polymorphism to extend this design to be
According to Javadoc, New implementations should consider using Iterator in preference to Enumeration If
I am wondering if there are any performance overhead issues to consider when using
Consider i am using join on three tables to get a desired result-set... Now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.