Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin绑定到属性_C#_Xamarin_Mvvm - Fatal编程技术网

C# Xamarin绑定到属性

C# Xamarin绑定到属性,c#,xamarin,mvvm,C#,Xamarin,Mvvm,因此,我有一个视图模型,具有如下属性,并具有必需的属性: [Required] public string Prop1 {get; set;} 我试图做的是创建一个如下所示的绑定集,用于检查属性是否具有必需的属性 <Entry x:Name="Prop1" BorderColor="{Somehow know if property has the required attribute}" Text="{Binding Prop1, Mode=TwoWay}"/> public

因此,我有一个视图模型,具有如下属性,并具有必需的属性:

[Required]
public string Prop1 {get; set;}
我试图做的是创建一个如下所示的绑定集,用于检查属性是否具有必需的属性

<Entry x:Name="Prop1" BorderColor="{Somehow know if property has the required attribute}"  Text="{Binding Prop1, Mode=TwoWay}"/>
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;
        }
    }
}

为了清楚起见,我需要能够从Xaml访问此文件


我希望这是有道理的。非常感谢您的帮助。

在您的模型中创建另一个属性,并使用此属性检查特定属性是否具有属性

<Entry x:Name="Prop1" BorderColor="{Somehow know if property has the required attribute}"  Text="{Binding Prop1, Mode=TwoWay}"/>
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;
        }
    }
}
在xaml中:

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

@Jason我知道如何从.cs访问属性。需要能够从XAML绑定到它您只能绑定到公共属性,因此您需要在您的模型上创建一个属性,以公开所需的
IsRequired
@Jason Ya希望有另一种方法,但哦,好吧。这个答案的问题如下:var pi=t.GetProperty(“Prop1”);我需要以某种方式导出Prop1。否则,我需要为我已经知道我可以做的每个属性执行所需的方法that@Maxqueue您可以将NameOfPrePrty设置为参数,并每次将其传递给该方法。另外,通过nameof()函数获取属性的名称。看见我更新了我的答案。不完全是我想说的,但是非常感谢你的努力。我希望它绑定时发生这种情况。换句话说,当我们说IsVisible=“{Binding IsRequired}”时,这可能会使用该属性返回true或false。好吧,恐怕您无法实现这一点。