I am having a requirment of changing the text color of each item inside the Picker dynamically for UWP APPs.
For example, Now the Picker shows the list of Names, so if the default flag is 0, I want to change the text color to red, if the Flag is 1 change the text color to green, etc. Is there a way to achieve this? Please suggest
CodePudding user response:
The picker's TextColor property can just change the selected item's text color. If you want to change the display items' text color, you can try to use the custom renderer.
I have created a sample and changed the item's text color. The picker in the xamarin is a control which inherited from the ComboBox in the uwp. So you can create a style to set the ComboBox's Foreground property.
Such as: in the app.xaml(uwp project part):
<Application
x:Class="AppTest.UWP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppTest.UWP">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="ComboBoxItem" x:Name="customtype">
<Setter Property="Foreground" Value="Blue"/>
</Style></ResourceDictionary>
</Application.Resources>
And in the customrender:
[assembly: ExportRenderer(typeof(Picker),typeof(MyPickerRender))]
namespace AppTest.UWP
{
public class MyPickerRender : PickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
Control.ItemContainerStyle = Windows.UI.Xaml.Application.Current.Resources["customtype"] as Windows.UI.Xaml.Style;
}
}
}
And because you want to set different color according to different value of the flag, so you can create two style and use different style according to the flag.