如何更改xamarin表单中的Picker底线颜色

时间:2018-02-22 06:35:00

标签: c# xaml xamarin

我需要更改选取器的底线颜色并更改选取器中显示的项目列表的背景颜色。我可以设置选取器的背景颜色。但选择器中项目的背景颜色默认为白色.Is有任何自定义渲染器或任何其他解决方案吗?我将选择器中的项添加为folllows。

以白色显示的血型列表。我需要更改该颜色。如何设置项目列表背景颜色?

public List<string> _bloodGroups = new List<string>() { "A+ve", "A-ve", "B+ve", "B-Ve", "O+ve", "O-ve", "AB+ve", "AB_ve" };

public void GetDistrictAsync()
{            
    foreach (string districts in _districts)
    {
        Picker_District.Items.Add(districts);
    }
}

在XAML页面中,代码如下:

<StackLayout Orientation="Horizontal" 
    HorizontalOptions="FillAndExpand" 
    VerticalOptions="FillAndExpand" 
    HeightRequest="10"
    Margin="20,10,20,0"  
    BackgroundColor="#91BC47">
    <Picker 
        x:Name="Picker_BloodGroup" 
        HorizontalOptions="FillAndExpand"  
        TextColor="White" 
        BackgroundColor="#91BC47">
        <Picker.Items>
            <x:String>Select Blood Group</x:String>                           
        </Picker.Items>
    </Picker>
</StackLayout>

2 个答案:

答案 0 :(得分:0)

您好@jasminemathew在xamarin ios部分中添加了此自定义渲染器 请记住在命名空间之前添加此行 [程序集:ExportRenderer(typeof(Picker),typeof(CustomPickerRenderer))]

 public class CustomPickerRenderer : PickerRenderer
{
    private CALayer _borderLayer;
    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
            return;

        Control.BorderStyle = UITextBorderStyle.None;

        var element = Element as Picker;
        if (element == null)
            return;

        //DrawBorder(element.BorderColor.ToCGColor());
        DrawBorder(UIColor.FromRGB(156, 156, 156));

        e.NewElement.Unfocused += (sender, evt) =>
        {
            DrawBorder(UIColor.FromRGB(156, 156, 156)); // unfocused, set color

        };
        e.NewElement.Focused += (sender, evt) =>
        {
            DrawBorder(UIColor.FromRGB(245, 0, 47)); ; // focused, set color
        };
    }
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        var element = Element as Picker;
        if (element == null)
            return;

        DrawBorder(UIColor.FromRGB(156, 156, 156));
    }
    public override CGRect Frame
    {
        get { return base.Frame; }
        set
        {
            base.Frame = value;

            var element = Element as Picker;
            if (element == null)
                return;

            // DrawBorder(element.BorderColor.ToCGColor());
            DrawBorder(UIColor.FromRGB(156, 156, 156));
        }
    }
    private void DrawBorder(UIColor borderColor)
    {
        if (Frame.Height <= 0 || Frame.Width <= 0)
            return;

        if (_borderLayer == null)
        {
            _borderLayer = new CALayer
            {
                MasksToBounds = true,
                Frame = new CGRect(0f, Frame.Height - 1, Frame.Width, 1f),
                BorderColor = borderColor.CGColor,
                BorderWidth = 1.0f
            };

            Control.Layer.AddSublayer(_borderLayer);
            Control.BorderStyle = UITextBorderStyle.None;
        }
        else
        {
            _borderLayer.BorderColor = borderColor.CGColor;
            _borderLayer.Frame = new CGRect(0f, Frame.Height - 1, Frame.Width, 1f);
        }
    }
}.   

答案 1 :(得分:-1)

protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
    base.OnElementChanged(e);

    if (Control != null && Element != null)
    {
        Control.Background = new SolidColorBrush(Colors.Transparent);

        Control.PlaceholderText = "Select";
        Control.BorderBrush = new SolidColorBrush(Colors.Transparent);
        Control.VerticalContentAlignment = VerticalAlignment.Center;
        Control.HorizontalContentAlignment = HorizontalAlignment.Left;
        Control.Foreground = new SolidColorBrush(Colors.Black);         
        var element = Element as BurndyRegPicker; 
        element.Title = string.Empty;?                

        element.Margin = new Xamarin.Forms.Thickness(-10, -6, -28, 4);

        }
    }
}