Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit fba6912

Browse files
Add ClearSelectedItem to the MaterialPicker
1 parent 6fdf4ae commit fba6912

5 files changed

Lines changed: 82 additions & 6 deletions

File tree

example/ExampleMaterialDesignControls/Pages/MaterialPickerPage.xaml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@
77
<ContentPage.Content>
88
<ScrollView>
99
<StackLayout Padding="16" Spacing="16">
10-
<material:MaterialPicker x:Name="pckColors" Type="Filled" LabelText="Color" AnimateError="True" />
10+
<material:MaterialPicker x:Name="pckColors" Type="Filled" LabelText="Color" AnimateError="True"
11+
Placeholder="Select a color"
12+
ItemsSource="{Binding ItemsSourceColors}" SelectedItem="{Binding SelectedItemColor}" />
13+
<Grid
14+
ColumnSpacing="16">
15+
<Grid.ColumnDefinitions>
16+
<ColumnDefinition Width="*" />
17+
<ColumnDefinition Width="*" />
18+
</Grid.ColumnDefinitions>
19+
<material:MaterialFlatButton
20+
Text="Clear"
21+
Command="{Binding ClearCommand}" />
22+
<material:MaterialButton
23+
Grid.Column="1"
24+
Text="Show"
25+
Command="{Binding ShowCommand}" />
26+
</Grid>
27+
1128
<material:MaterialPicker Type="Filled" LabelText="Color" AnimateError="True" IsEnabled="False" />
1229
<material:MaterialPicker x:Name="pckSizes" SelectedItem="{Binding SelectedSizes}" Type="Outlined" LabelText="Size" Placeholder="Select option" PlaceholderColor="Red" />
1330
<material:MaterialPicker x:Name="pckModels" Type="Lined" LabelText="Model" LeadingIcon="color.png" />

example/ExampleMaterialDesignControls/Pages/MaterialPickerPage.xaml.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ namespace ExampleMaterialDesignControls.Pages
88
{
99
public partial class MaterialPickerPage : ContentPage
1010
{
11+
public List<string> ItemsSourceColors { get; set; }
12+
13+
public string SelectedItemColor { get; set; }
14+
1115
public string SelectedSizes { get; set; }
1216

1317
public string SelectedItem { get; set; }
@@ -22,7 +26,8 @@ public MaterialPickerPage()
2226
{
2327
InitializeComponent();
2428

25-
this.pckColors.ItemsSource = new List<string> { "Red", "Blue", "Green" };
29+
ItemsSourceColors = new List<string> { "Red", "Blue", "Green" };
30+
2631
this.pckSizes.ItemsSource = new List<string> { "P", "M", "X", "XL" };
2732

2833
this.pckModels.ItemsSource = new List<string> { "Model A", "Model B", "Model C", "Model D" };
@@ -44,6 +49,19 @@ public MaterialPickerPage()
4449

4550
this.Tap3Command = new Command<string>(OnTap3);
4651

52+
ClearCommand = new Command(() =>
53+
{
54+
pckColors.ClearSelectedItem();
55+
});
56+
57+
ShowCommand = new Command(async () =>
58+
{
59+
if (!string.IsNullOrEmpty(SelectedItemColor))
60+
await DisplayAlert("Color", SelectedItemColor, "Ok");
61+
else
62+
await DisplayAlert("Color", "No color selected", "Ok");
63+
});
64+
4765
this.BindingContext = this;
4866
}
4967

@@ -85,5 +103,9 @@ public async void OnTap3(object parameter)
85103
{
86104
this.pckDoubleWithFocus.Focus();
87105
}
106+
107+
public ICommand ClearCommand { get; set; }
108+
109+
public ICommand ShowCommand { get; set; }
88110
}
89-
}
111+
}

src/MaterialDesignControls.Android/Renderers/MaterialPickerRenderer.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
2-
using Android.Content;
1+
using Android.Content;
32
using Android.Graphics.Drawables;
43
using AndroidGraphics = Android.Graphics;
54
using Plugin.MaterialDesignControls.Implementations;
65
using Xamarin.Forms;
76
using Xamarin.Forms.Platform.Android;
87
using Plugin.MaterialDesignControls.Android.Utils;
8+
using System.ComponentModel;
99

1010
[assembly: ExportRenderer(typeof(CustomPicker), typeof(Plugin.MaterialDesignControls.Android.MaterialPickerRenderer))]
1111

@@ -39,5 +39,21 @@ protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.P
3939
}
4040
}
4141
}
42+
43+
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
44+
{
45+
base.OnElementPropertyChanged(sender, e);
46+
47+
var customPicker = (CustomPicker)Element;
48+
if (e.PropertyName == nameof(customPicker.SelectedIndex))
49+
{
50+
if (customPicker.SelectedItem == null && !string.IsNullOrEmpty(customPicker.Placeholder))
51+
{
52+
this.Control.Text = null;
53+
this.Control.Hint = customPicker.Placeholder;
54+
this.Control.SetHintTextColor(customPicker.PlaceholderColor.ToAndroid());
55+
}
56+
}
57+
}
4258
}
43-
}
59+
}

src/MaterialDesignControls.iOS/Renderers/MaterialPickerRenderer.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.P
4646
}
4747
}
4848
}
49+
50+
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
51+
{
52+
base.OnElementPropertyChanged(sender, e);
53+
54+
var customPicker = (CustomPicker)Element;
55+
if (e.PropertyName == nameof(customPicker.SelectedIndex))
56+
{
57+
if (customPicker.SelectedItem == null && !string.IsNullOrEmpty(customPicker.Placeholder))
58+
{
59+
this.Control.Text = null;
60+
this.Control.AttributedPlaceholder = new NSAttributedString(customPicker.Placeholder, foregroundColor: customPicker.PlaceholderColor.ToUIColor());
61+
}
62+
}
63+
}
4964
}
5065

5166
public class MyPickerDelegate : UIPickerViewDelegate

src/MaterialDesignControls/Controls/MaterialPicker.xaml.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,12 @@ private void PckOptions_SelectedIndexChanged(object sender, EventArgs e)
337337
}
338338
}
339339

340+
public void ClearSelectedItem()
341+
{
342+
SelectedItem = null;
343+
pckOptions.SelectedIndex = -1;
344+
}
345+
340346
#endregion Methods
341347
}
342348
}

0 commit comments

Comments
 (0)