Xamarin 如何绑定行以便在.cs文件中将isVisible设置为false?

Xamarin 如何绑定行以便在.cs文件中将isVisible设置为false?,xamarin,xamarin.android,Xamarin,Xamarin.android,即使我在上面加了一个名字,我似乎也无法访问它,因为它在listview中。如果我希望第1行在代码后面不可见,我该如何处理 我试图为标签指定一个名称,但无法访问它。或者添加了我无法访问的代码隐藏 <ListView x:Name="postListView" SeparatorVisibility="Default" HasUnevenRows="True" ItemsSource="{Binding Items}" SeparatorColor="White"> <Li

即使我在上面加了一个名字,我似乎也无法访问它,因为它在listview中。如果我希望第1行在代码后面不可见,我该如何处理

我试图为标签指定一个名称,但无法访问它。或者添加了我无法访问的代码隐藏

<ListView x:Name="postListView" SeparatorVisibility="Default" HasUnevenRows="True" ItemsSource="{Binding Items}" SeparatorColor="White">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid BackgroundColor="Black" HorizontalOptions="CenterAndExpand"
                      VerticalOptions="FillAndExpand" Padding="1,2,1,0">
                <Grid HorizontalOptions="CenterAndExpand" 
                      VerticalOptions="FillAndExpand" ColumnSpacing="1" RowSpacing="1">
                    <Grid.RowDefinitions >
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="120"/>
                        <ColumnDefinition Width="100"/>
                    </Grid.ColumnDefinitions>

                    <Label Grid.Row="0" FontSize="Medium" Grid.Column="0" Text="right tst:" HorizontalTextAlignment="Start" BackgroundColor="cornflowerblue" />
                    <Label Grid.Column="1" Grid.Row="0" Text="{Binding drain1vol}" HorizontalTextAlignment="Center" BackgroundColor="cornflowerblue"/>
                    <Label Grid.Row="1" Grid.Column="0" Text="nothing"  BackgroundColor="Yellow"/>
                    <Label Grid.Row="1" Grid.Column="1" Text="{Binding drain2vol}" HorizontalTextAlignment="Center" BackgroundColor="Yellow" />
                    </Grid>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

//Model and Source of data
using System;
using SQLite;
using Demo.Helpers;

namespace Demo.Model

{

//this is the source of Binding
public class Post
{

    //ID primary key that we will autoincrement   
    //These are binding source for Historypage
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }

    public bool showLabel { get; set; }  //public class model
}
}

//模型和数据源
使用制度;
使用SQLite;
使用Demo.Helpers;
名称空间Demo.Model
{
//这是绑定的来源
公营职位
{
//我们将自动递增的ID主键
//这些是Historypage的绑定源
[主密钥,自动增量]
公共int ID{get;set;}
公共bool showLabel{get;set;}//公共类模型
}
}

源是Post类。

将标签的
IsVisible
属性绑定到模型中的属性,以控制标签的可视性

比如说,, 在
xaml
中:

 <Label Grid.Column="1" Grid.Row="0" IsVisible="{Binding showLabel}" Text="{Binding drain1vol}" HorizontalTextAlignment="Center" BackgroundColor="cornflowerblue"/>
创建数据源时,可以设置标签的
isVisable

    Items.Add(new model { drain1vol = "Rob Finnerty" ,showLabel= false });
    Items.Add(new model { drain1vol = "Bill Wrestler", showLabel = true });
    Items.Add(new model { drain1vol = "Dr. Geri-Beth Hooper", showLabel = false });
    Items.Add(new model { drain1vol = "Dr. Keith Joyce-Purdy", showLabel = true });
    Items.Add(new model { drain1vol = "Sheri Spruce", showLabel = false });

    postListView.ItemsSource = Items;
要更改代码隐藏中的
可查看的
功能,请执行以下操作:

void test() {

    //Get the model you want to change
    model.showLabel = false / true;
}
更新:

在模型中实现INotifyPropertyChanged
接口:

class model : INotifyPropertyChanged
{
    private bool showLabel { get; set; }

    private string drain1vol { get; set; }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;



    public bool ShowLabel
    {
        set
        {
            if (showLabel != value)
            {
                showLabel = value;
                OnPropertyChanged("ShowLabel");     
            }
        }
        get
        {
            return showLabel;
        }
    }

    public string Drain1vol
    {
        set
        {
            if (drain1vol != value)
            {
                drain1vol = value;
                OnPropertyChanged("Drain1vol");
            }
        }
        get
        {
            return drain1vol;
        }
    }
}
在xaml中,绑定到
ShowLabel
drainVol
(大写):



这是对您的另一个问题的回答的准确引用:“您不能从代码隐藏中访问模板内的元素。请使用数据绑定来设置模板元素的属性。”是否有方法访问该行而不是访问标签?您知道,就像gridrow1.isVisible=false后面的代码一样;还看得见吗?并且在代码隐藏Post.showLabel=false@hypnocool如果要隐藏行,请将rowHeight设置为0以实现此目的。检查这些线程:在更新的代码后面,我应该使用小写model.showLabel=false/true;或model.ShowLabel=false/true;如果大写我不能访问它,它必须是静态的,但如果我在公共静态boolshowlab上添加静态,它就会出错@hypnocool为什么你可以;无法访问大写属性?这是公开的,就用我的模型吧。
class model : INotifyPropertyChanged
{
    private bool showLabel { get; set; }

    private string drain1vol { get; set; }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;



    public bool ShowLabel
    {
        set
        {
            if (showLabel != value)
            {
                showLabel = value;
                OnPropertyChanged("ShowLabel");     
            }
        }
        get
        {
            return showLabel;
        }
    }

    public string Drain1vol
    {
        set
        {
            if (drain1vol != value)
            {
                drain1vol = value;
                OnPropertyChanged("Drain1vol");
            }
        }
        get
        {
            return drain1vol;
        }
    }
}
 <Label Grid.Column="1" Grid.Row="0" IsVisible="{Binding ShowLabel}" Text="{Binding Drain1vol}" HorizontalTextAlignment="Center" BackgroundColor="cornflowerblue"/>