XAML 获取其他对象属性的值

czfnxgou  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(169)

我有轮播视图,我想使用XAML获取轮播中下一个对象的属性颜色。我的XAML:

<CarouselView
    HeightRequest="300"
    PeekAreaInsets="100"
    IndicatorView="indicatorView"
    ItemsSource="{Binding TrafficColors}">
    <CarouselView.ItemTemplate>
        <DataTemplate>
            <StackLayout>
                <Frame HasShadow="False"
                       CornerRadius="5"
                       Padding="0"
                       Margin="10, 0"
                       WidthRequest="200"
                       HeightRequest="300"
                       >
                    <Frame.Background>
                        <LinearGradientBrush EndPoint="1,1">
                            <GradientStop Color="{Binding HEX, StringFormat='#{0}'}" Offset="0.4" />
                            <GradientStop Color="#CFDEF3" Offset="0.77" />
                        </LinearGradientBrush>
                    </Frame.Background>
                    <!--
                    <Frame.GestureRecognizers>
                        <TapGestureRecognizer 
                            Command="{Binding Source={RelativeSource AncestorType={x:Type view:EDocsMainPageViewModel}}, Path=GoToEDoc}"
                            CommandParameter="{Binding Id}" 
                        />
                    </Frame.GestureRecognizers>
                    -->
                    <StackLayout>
                        <Label Text="{Binding Name}"
                               TextColor="Black"
                               FontSize="22"
                               Padding="10"
                               HorizontalOptions="Center"
                               VerticalOptions="Center" />
                        <Label Padding="10" Text="{Binding ShiftCode, StringFormat='Номер МЛ:{0}'}" TextColor="Black" FontSize="14" />
                        <Label Padding="10" Text="{Binding RegDate, StringFormat='Дата:{0:yyyy.MM.dd HH:mm}'}" TextColor="Black" FontSize="14" />
                    </StackLayout>
                </Frame>
            </StackLayout>
        </DataTemplate>
    </CarouselView.ItemTemplate>
</CarouselView>

例如,我想得到集合中下一个FrameBackgroundColor。例如,在HTML中,我有对象ID或类名,通过这个键和JS我可以得到一些东西。那么XAML呢?我可以为集合中的对象设置键,并通过这个键以某种方式从XAML中得到值吗?我将感谢任何信息。
另外,我不想在视图模型中再添加一个属性,我想在XAML中添加。

hpxqektj

hpxqektj1#

例如,我想获取集合中下一个Frame的BackgroundColor。例如,在HTML中,我有对象ID或类名,通过这些键和JS,我可以得到一些东西。那么XAML呢?我可以为集合中的对象设置键,然后通过这个键以某种方式从XAML中获取值吗?
我们无法得到集合中下一个FrameBackgroundColor,因为我们不知道将选择哪一项。但是我们可以得到currentItempreviousItem
我们知道CarouselView的项使用CarouselView.ItemTemplate的相同的DataTemplate,所以我们不能通过DataTemplate布局中视图的x:Name得到下一个FrameBackgroundColor
既然你把bind的颜色传给属性Frame.Background,那么你就可以通过事件OnCurrentItemChanged得到currentItempreviousItem
因此,我建议在ItemsSourceTrafficColors)的Item中添加一个属性(例如BackgroundColor)。

public string BackgroundColor { get; set; }

然后,您将在事件OnCurrentItemChanged中获得currentItempreviousItem
例如:

void OnCurrentItemChanged(object sender, CurrentItemChangedEventArgs e)
    {
// here you can get the PreviousItem and CurrentItem which include the property `BackgroundColor`
        previousItem = e.PreviousItem as Item;
        currentItem = e.CurrentItem as Item;
        
    }

TestPage.xaml

<CarouselView ItemsSource="{Binding Monkeys}"
                  CurrentItemChanged="OnCurrentItemChanged"
                  PositionChanged="OnPositionChanged">
        <CarouselView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Frame HasShadow="True"
                           BorderColor="DarkGray"
                           CornerRadius="5"
                           Margin="20"
                           HeightRequest="300"
                           HorizontalOptions="Center"
                           VerticalOptions="Center"
                           
                           BackgroundColor="{Binding BackgroundColor, Converter = {StaticResource ColorConverter}}"
                           >
                        <StackLayout>
                            <Label Text="{Binding Name}" 
                                   FontAttributes="Bold"
                                   FontSize="20"
                                   HorizontalOptions="Center"
                                   VerticalOptions="Center" />
                            <Image Source="{Binding ImageUrl}" 
                                   Aspect="AspectFill"
                                   HeightRequest="150"
                                   WidthRequest="150"
                                   HorizontalOptions="Center" />
                            <Label Text="{Binding Location}"
                                   HorizontalOptions="Center" />
                            <Label Text="{Binding Details}"
                                   FontAttributes="Italic"
                                   HorizontalOptions="Center"
                                   MaxLines="5"
                                   LineBreakMode="TailTruncation" />
                        </StackLayout>
                    </Frame>
                </StackLayout>
            </DataTemplate>
        </CarouselView.ItemTemplate>
    </CarouselView>

有关CarouselView的详细信息,请查看:Configure CarouselView interaction
关于如何绑定颜色,可以查看线程:https://stackoverflow.com/questions/72901517/net-maui-how-to-reference-a-color-in-a-binding。

相关问题