首页 > 解决方案 > 自定义所选元素的外观

问题描述

是否有可能在指标视图中自定义活动/选定项目的外观?使用数据模板选择器还是什么?数据模板会更改每一项。

我的目标是达到这个效果:

第二项活动

第三项活动

标签: xamarin.formsxamarin.androidxamarin.iosindicator

解决方案


当您的视图的颜色变化增加视图的宽度时尝试使用触发器,或者您可以使用视图的 PropertyChanged 事件来检测视图的变化。我今天有类似的要求,下面的代码对我有用。

<IndicatorView
                x:Name="introductionIndicator"
                Grid.Row="2"
                HorizontalOptions="Center"
                IndicatorColor="{StaticResource PrimaryLightGrayColor}"
                SelectedIndicatorColor="{StaticResource PrimaryYellowColor}">
                <IndicatorView.IndicatorTemplate>
                    <DataTemplate>
                        <BoxView
                            CornerRadius="10"
                            HeightRequest="6"
                            WidthRequest="20">
                            <BoxView.Triggers>
                                <Trigger TargetType="BoxView" Property="BackgroundColor" Value="{StaticResource PrimaryYellowColor}">
                                    <Setter Property="WidthRequest" Value="30" />
                                </Trigger>
                            </BoxView.Triggers>
                        </BoxView>
                    </DataTemplate>
                </IndicatorView.IndicatorTemplate>
            </IndicatorView>

结果: 在此处输入图像描述


推荐阅读