更改选中的列表视图项目 Xamarin.Forms 的项目控制 属性

Change item control property of checked listview item Xamarin.Forms

嗨, 如果选中项目复选框并隐藏它,我试图显示评论输入,我有这个 XAML

<ListView ItemsSource="{Binding TaskItems}" x:Name="TasksItems" HasUnevenRows="True" VerticalScrollBarVisibility="Default" SelectionMode="None">
<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <StackLayout IsVisible="True" Orientation="Vertical">
                <Grid BackgroundColor="White">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                    </Grid.RowDefinitions>

                    <StackLayout Grid.Column="0" Grid.Row="0">
                        <input:CheckBox Type="Box" IsChecked="{Binding TaskChecked , Mode=TwoWay}"/>
                    </StackLayout>

                    <StackLayout Grid.Column="0" Grid.Row="1" IsVisible="{Binding CommentRequired}">
                        <Entry BackgroundColor="White" PlaceholderColor="Black" HeightRequest="40" TextColor="Black"/>
                    </StackLayout>
                </Grid>
            </StackLayout>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>

我有这个 c# 代码可以从数据库中读取项目

public class TaskData
{
    public bool TaskChecked { get; set; }
    public bool CommentRequired { get; set; }
}

public partial class HomePage : ContentPage
{
    public HomePage()
    {
        ObservableCollection<TaskData> TaskItems { get; set; }
        ObservableCollection<TaskData> TasksList;
        TaskItems = new ObservableCollection<TaskData>();

        //Loop the tasks from database result
        While...
           TaskItems.Add(new TaskData
           {
               TaskChecked = false,
               CommentRequired = false,
           });
        //End Loop

        TasksListView.ItemsSource = TaskItems;
    }
}

现在我需要添加一个“CheckChanged”事件以在用户选中目标列表视图项的复选框时显示评论条目 (IsVisible="True")

谢谢

首先添加事件。

<input:CheckBox Type="Box" IsChecked="{Binding TaskChecked , Mode=TwoWay}" CheckedChanged="OnCheckBoxCheckedChanged"/>

为第二个堆栈添加名称

<StackLayout x:Name="StackLayoutEntry" Grid.Column="0" Grid.Row="1" IsVisible="{Binding CommentRequired}">
<Entry BackgroundColor="White" PlaceholderColor="Black" HeightRequest="40" TextColor="Black"/>
</StackLayout>

然后在后面的代码中使用这个函数来找到点击复选框的入口

void OnCheckBoxCheckedChanged(object sender, CheckedChangedEventArgs e)
{
   var Sender = (CheckBox)sender;
   var stacklayoutentry = Sender.Parent.FindByName<StackLayout>("StackLayoutEntry");
   stacklayoutentry.IsVisible = True;

}

这也可能对您有所帮助 Check

另一种方法,但您需要为所选项目提供标识符。 Check