【问题标题】:How can I bind this Icon's Color to this Label Color?如何将此图标的颜色绑定到此标签颜色?
【发布时间】:2021-04-01 20:10:00
【问题描述】:

如下所示,FlyoutItem 有一个 Icon 和一个 Title

    <FlyoutItem Title="About" >
        <FlyoutItem.Icon>
            <FontImageSource FontFamily="MaterialDesignIconFont"
                            Glyph="{StaticResource InformationOutlineGlyph}"
                            Color="Green" />
        </FlyoutItem.Icon>

        <ShellContent Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
    </FlyoutItem>

Title 颜色会因为这个Style 而自动改变:

<Style Class="FlyoutItemLayoutStyle" TargetType="Layout" ApplyToDerivedTypes="True">
    <Setter Property="BackgroundColor" Value="LightBlue"></Setter>
    <Setter Property="VisualStateManager.VisualStateGroups">
        <VisualStateGroupList>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal">
                    <VisualState.Setters>
                        <Setter Property="BackgroundColor" Value="White" />
                        <Setter TargetName="FlyoutItemLabel" Property="Label.TextColor" Value="{StaticResource PrimaryColor}" />
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="Selected">
                    <VisualState.Setters>
                        <Setter Property="BackgroundColor" Value="{StaticResource PrimaryColor}" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateGroupList>
    </Setter>
</Style>

所以LabelTitle 在选择FlyoutItem 时会自动改变颜色。我需要Icon 来做同样的事情。我可以使用触发器来设置FontImageSource,但这有它自己的问题。

鉴于上述Style 中的TargetName“FlyoutItemLabel”,是否可以创建从FontImageSource.Color 到每个FlyoutItem"FlyoutItemLabel".Color 的绑定?它必须向上绑定到FlyoutItem 祖先,然后向下绑定到&lt;Label x:Name="FlyoutItemLabel" /&gt;,不是吗?

【问题讨论】:

    标签: xaml xamarin.forms data-binding xamarin.forms.shell


    【解决方案1】:

    您可以根据需要设置 FlyoutItem[ItemTemplate][1]

    <Shell.ItemTemplate>
            <DataTemplate >
                <Grid Style="{StaticResource FloutItemStyle}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.2*" />
                        <ColumnDefinition Width="0.8*" />
                    </Grid.ColumnDefinitions>
                    <Image Source="{Binding xxx}"
            Margin="5"
            BackgroundColor="{Binding Source={x:Reference label} ,Path=BackgroundColor}"
            HeightRequest="45" />
                    <Label Grid.Column="1"
                           x:Name="label"
                           BackgroundColor="Red"
            Text="{Binding Title}"
            FontAttributes="Italic"
            VerticalTextAlignment="Center" />
                </Grid>
            </DataTemplate>
        </Shell.ItemTemplate>
    

    【讨论】:

    猜你喜欢
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    相关资源
    最近更新 更多