0

I'm trying to get Caliburn Micro going in my project and I'm having trouble understanding guard methods (Can*) in the context of properties that aren't bound to the view (forgive me if I'm getting my terminology wrong).

I've adapted Tim Corey's example to add this snippet:

        private bool _connected;

        public bool Connected
        {
            get { return _connected; }
            set { _connected = value; NotifyOfPropertyChange(() => Connected); }
        }

        public bool CanSayHello(bool connected)
        {
            return connected;
        }

        public void SayHello(bool connected)
        {
            Console.WriteLine("Hello!");
        }

        public bool CanClearText(string firstName, string lastName)
        {
            if (String.IsNullOrWhiteSpace(firstName) && string.IsNullOrWhiteSpace(lastName))
            {
                Connected = false;
                return false;
            }
            else
            {
                Connected = true;
                return true;
            }
        }

And the associated xaml is:

        <!-- Row 4 -->
        <Button x:Name="ClearText" Grid.Row="4" Grid.Column="1">Clear Names</Button>
        <Button x:Name="SayHello" Grid.Row="4" Grid.Column="2">Say Hello</Button>

The SayHello button is never enabled (even though it seems like it should have the same state as CanClearText). The intent is to use Connected as an intermediary property; in the application I'm putting together, the idea is that Connected is actually set by an external Message (from a model--not connected to a view).

This question gets to it a bit, but I'd like to avoid explicitly calling NotifyOfPropertyChange(() => CanSayHello); and let the Caliburn Micro framework do the work for me. I'm pretty new to this, and I'm sure there's something simple I'm missing--thanks for your help.

watashi16
  • 145
  • 2
  • 6

1 Answers1

0

Caliburn Micro, in this particular, case might not provide a easy solution. You would have to trigger the NotifyPropertyChanged manually for the dependend properties like CanSayHello each time Connected Property changes. However, you could make use of Fody Extensions which could do the same for you, without explicitly typing the code.

For example,

[AlsoNotify(nameof(CanSayHello))]
public bool Connected
{
    get { return _connected; }
        set { _connected = value; NotifyOfPropertyChange(() => Connected); }
}

This, with the help of Code weaving would do the NotifyPropertyChanged for without having to explicitly do so yourself.

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51