Как вы можете видеть ниже, у FlyoutItem
есть значок и 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>
Таким образом, Label
, то есть Title
, автоматически меняет цвет при выборе FlyoutItem
. Мне нужно Icon
, чтобы сделать то же самое. Я мог бы использовать триггер для установки FontImageSource
, но это сопряжено со своими проблемами.
Учитывая TargetName
«FlyoutItemLabel» из приведенного выше Style
, возможно ли создать привязку от FontImageSource.Color
к каждому FlyoutItem
"FlyoutItemLabel".Color
? Он должен был бы привязываться вверх к предку FlyoutItem
, а затем вниз к предку <Label x:Name = "FlyoutItemLabel" />
, не так ли?
Вы можете установить [ItemTemplate][1]
FlyoutItem, как хотите.
<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>
Проверьте learn.microsoft.com/en-us/xamarin/xamarin-forms/…
Извините, но я не понимаю, куда этот код можно было бы использовать, чтобы его можно было использовать повторно. Будет ли это
Style
таргетинг на типFlyoutItem
?