XAML 如何在.NET Maui中将CollectionView对象传递给自定义控件?

zzoitvuj  于 2022-12-07  发布在  .NET
关注(0)|答案(1)|浏览(217)

I'm trying to leverage a custom control within a CollectionView and would like to pass the entire object of the particular CollectionView ItemTemplate into my custom control.
Here's my xaml page:

<CollectionView ItemsSource="{Binding WorkOps}" SelectionMode="None" ItemsLayout="VerticalList">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="75" />
                    <ColumnDefinition Width="15" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>

                <Label Grid.Column="0" 
                    Text="{Binding OpType}" 
                    FontSize="Caption" 
                    VerticalTextAlignment="Center"/>
                <Label Grid.Column="1" 
                    Text="{Binding OpNumber}" 
                    FontSize="Caption" 
                    VerticalTextAlignment="Center"/>
                <Label Grid.Column="2" 
                    Text="{Binding Instructions}" 
                    FontSize="Body"/>

                <Entry Grid.Column="2" 
                    Grid.Row="1" 
                    Text="{Binding Measure}"
                    IsVisible="{Binding IsSimpleMeasure}" />

                <root:TableMeasureView Grid.Column="2" 
                                    Grid.Row="1" 
                                    Op="{Binding .}" 
                                    IsVisible="{Binding IsTableMeasure}" />

            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

and here is my custom control I'm trying to implement:

public class TableMeasureView : Grid
{
    public static readonly BindableProperty WorkOpProperty =
        BindableProperty.Create(nameof(Op), typeof(WorkOp), typeof(ContentPage));

    public WorkOp Op
    {
        get { return (WorkOp)GetValue(WorkOpProperty); }
        set { SetValue(WorkOpProperty, value); }
    }

    public TableMeasureView()
    {
    }

    // ...
}

I get the following message when trying to build:
XamlC error XFC0009: No property, BindableProperty, or event found for "Op", or mismatching type between value and property.
Is what I'm attempting possible?

vkc1a9a2

vkc1a9a21#

是的,这是可能的。发生的事情是xaml不试图找出{Binding .}的类型是WorkOp。它想要一个object类型的属性。
修复方法是为其给予object类型的属性。然后,为了方便在自定义控件中访问,创建第二个属性,将其转换为WorkOp

public static readonly BindableProperty WorkOpProperty =
        BindableProperty.Create(nameof(Op), typeof(object), typeof(ContentPage));

public object Op
{
    get { return GetValue(WorkOpProperty); }
    set { SetValue(WorkOpProperty, value); }
}

private WorkOp TypedOp => (WorkOp)Op;

注意:将上面的OpTypedOp更改为您喜欢的任何名称。如果您更改Op,请记住也更改引用它的xaml。

相关问题