0

So i have a view model with property like following with a required attribute:

[Required]
public string Prop1 {get; set;}

What i am trying to do is have a binding set like following that checks if the property has the required attribute.

<Entry x:Name="Prop1" BorderColor="{Somehow know if property has the required attribute}"  Text="{Binding Prop1, Mode=TwoWay}"/>

To be clear I need to be able to access this from Xaml

I hope this makes sense. Any help would be much appreciated.

Maxqueue
  • 2,194
  • 2
  • 23
  • 55

1 Answers1

1

Create another property in your model and use this property to check if the specific property has attribute.

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        this.BindingContext = new myViewModel();
    }
}

class myViewModel : INotifyPropertyChanged
{
    bool _isRequired;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Prop1 { get; set; }

    public myViewModel()
    {
        checkIfRequired();
    }

    public void checkIfRequired() {

        var t = typeof(myViewModel);
        var pi = t.GetProperty("Prop1");
        bool hasIsIdentity = Attribute.IsDefined(pi, typeof(RequiredAttribute));

        isRequired = hasIsIdentity;
    }

    public bool isRequired
    {
        set
        {
            if (_isRequired != value)
            {
                _isRequired = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("isRequired"));
                }
            }
        }
        get
        {
            return _isRequired;
        }
    }
}

In xaml:

<Entry x:Name="Prop1" IsEnabled="{Binding isRequired}"  Text="{Binding Prop1, Mode=TwoWay}"/>

You may need to use a converter if you need to bind to the BorderColor.

Update:

public myViewModel()
{
    checkIfRequired(nameof(Prop1));
}

public void checkIfRequired(string nameOfProeprty) {

    var t = typeof(myViewModel);
    var pi = t.GetProperty(nameOfProeprty);
    bool hasIsIdentity = Attribute.IsDefined(pi, typeof(RequiredAttribute));

    isRequired = hasIsIdentity;
}
nevermore
  • 15,432
  • 1
  • 12
  • 30
  • So problem with this answer is following: var pi = t.GetProperty("Prop1"); I need Prop1 to be derived somehow. Otherwise need to do IsRequired method for every property which i already knew i could do that – Maxqueue Apr 09 '20 at 01:17
  • @Maxqueue You can set the nameOfProeprty as a parameter and pass it to the method everytime. Also, get the name of property through nameof() function. See [this answer](https://stackoverflow.com/a/31103473/10539446). I updated my answer. – nevermore Apr 09 '20 at 05:39
  • Not quite what i was getting at but your efforts are much appreciated. I wanted this to occur when it binds. In other words when lets say IsVisible="{Binding IsRequired}" then this could use that attribute to return true or false. – Maxqueue Apr 09 '20 at 15:00
  • Well, I'm afraid you can't achieve that. – nevermore Apr 10 '20 at 01:24