【问题标题】:Issue with VisualStateManager for CollectionView in Xamarin FormsXamarin 表单中 CollectionView 的 VisualStateManager 问题
【发布时间】:2021-10-26 11:08:55
【问题描述】:

我对 CollectionView 和 VisualStateManager 有疑问。 这是我的收藏视图。我也尝试使用编译绑定。

    <CollectionView Grid.Row="1"
                        ItemSizingStrategy="MeasureAllItems"
                        ItemsSource="{Binding SizeOptions}"
                        Margin="0"
                        SelectionChangedCommand="{Binding SelectionChangedCommand}"
                        SelectedItem="{Binding SelectedSizeOption}"
                        SelectionMode="Single">
            <CollectionView.Header>
                <BoxView VerticalOptions="Start"
                         HeightRequest="1"
                         Color="{StaticResource DividerColor}"/>
            </CollectionView.Header>
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="tpViewModels:SizeOption">
                    <StackLayout IsEnabled="{Binding IsEnabled}">
                        <StackLayout AutomationId="{Binding Text}"
                                     AutomationProperties.IsInAccessibleTree="True"
                                     HeightRequest="64"
                                     IsEnabled="{Binding IsEnabled}"
                                     Margin="10, 0, 0, 0"
                                     MinimumHeightRequest="64"
                                     Orientation="Horizontal"
                                     Padding="0"
                                     x:Name="Holder">
                            <Image HeightRequest="20"
                                   IsEnabled="{Binding IsEnabled}"
                                   WidthRequest="20"
                                   x:Name="RadioButtonImage">
                            </Image>
                            <Label FontSize="14"
                                   HorizontalOptions="StartAndExpand"
                                   IsEnabled="{Binding IsEnabled}"
                                   Padding="10, 0"
                                   Text="{Binding Text}"
                                   Style="{StaticResource MediumFontFamily}"
                                   VerticalTextAlignment="Center"
                                   x:Name="RadioButtonLabel">
                            </Label>
                        </StackLayout>
                        <BoxView VerticalOptions="Start"
                                 HeightRequest="1"
                                 Color="{StaticResource DividerColor}"/>
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

当我在 DataTemplate 中不使用 DataType 时,IsEnabled = false 完美,我们无法选择列表中的项目。但是当我使用 DataType 时,我可以选择禁用项。 我还想在禁用项目时更改 TextColor、FontAttributes 和 Image.Source。

它只为我设置 VisualStates = Normal 或 Selected,VisualStateManager 不适用于 State = Disabled。 我在第一个 StackLayout 中添加了 VisualStateManager,TargetName 设置为 x:Name of Label 和 Image。

DataTrigger for Label 和 Image 有效,但我可以选择禁用的项目 :(

有人知道为什么吗?

【问题讨论】:

    标签: xamarin.forms collectionview datatrigger visualstatemanager


    【解决方案1】:

    您可以尝试将Visual State Manager 设置为ContentPage.Resources

    喜欢:

    <ContentPage.Resources>
        <Style TargetType="Label">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal" >
                            <VisualState.Setters>
                                <Setter Property="TextColor"
                                        Value="LightSkyBlue" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="TextColor"
                                        Value="LightSkyBlue" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Disabled">
                            <VisualState.Setters>
                                <Setter Property="TextColor"
                                        Value="Green" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
        <Style TargetType="Image">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal" >
                            <VisualState.Setters>
                                <Setter Property="Source"
                                        Value="normalicon.png" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="Source"
                                        Value="normalicon.png" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Disabled">
                            <VisualState.Setters>
                                <Setter Property="Source"
                                        Value="disableicon.png" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
    </ContentPage.Resources>
    

    【讨论】:

      猜你喜欢
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 2021-01-22
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      相关资源
      最近更新 更多