Skip to content

Commit

Permalink
Display SegmentedControlOptions with missing TextPropertyName
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasgalliker committed Jul 24, 2024
1 parent 4299001 commit a82864a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
21 changes: 18 additions & 3 deletions Plugin.SegmentedControl.Maui/Controls/SegmentedControlOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,25 @@ private void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)

private void SetTextFromItemProperty()
{
if (this.Item != null && this.TextPropertyName != null)
if (this.Item is object item)
{
var propertyInfo = this.Item.GetType().GetProperty(this.TextPropertyName);
this.Text = propertyInfo?.GetValue(this.Item)?.ToString();
if (this.TextPropertyName is string textPropertyName && !string.IsNullOrEmpty(textPropertyName))
{
var itemType = item.GetType();
var propertyInfo = itemType.GetProperty(textPropertyName);
if (propertyInfo == null)
{
throw new ArgumentException($"Property '{textPropertyName}' could not be found on object of type {itemType.FullName}", nameof(this.TextPropertyName));
}
else
{
this.Text = propertyInfo.GetValue(item)?.ToString();
}
}
else
{
this.Text = item.ToString();
}
}
}
}
Expand Down
22 changes: 17 additions & 5 deletions Samples/SegmentedControlDemoApp/Views/Test1Page.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,31 @@
Spacing="20"
VerticalOptions="Start">

<Label Text="Example with an embedded string array as segment options defined in XAML. All style properties are left with default values." />
<Label Text="Example with an embedded string array as segment options defined in XAML.&#10;All style properties are left with default values." />

<s:SegmentedControl>
<s:SegmentedControl.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Baboon</x:String>
<x:String>Capuchin Monkey</x:String>
<x:String>Blue Monkey</x:String>
<x:String>Tab 1</x:String>
<x:String>Tab 2</x:String>
<x:String>Tab 3</x:String>
</x:Array>
</s:SegmentedControl.ItemsSource>
</s:SegmentedControl>

<Label Text="Example with SegmentedControlOptions defined in XAML. Events subscribed in code-behind. No styles applied. WidthRequest=160." />
<Label Text="Example with an array of SegmentedControlOptions set as ItemsSource." />

<s:SegmentedControl>
<s:SegmentedControl.ItemsSource>
<x:Array Type="{x:Type s:SegmentedControlOption}">
<s:SegmentedControlOption Text="Tab1" />
<s:SegmentedControlOption Text="Tab2" />
<s:SegmentedControlOption Text="Tab3" />
</x:Array>
</s:SegmentedControl.ItemsSource>
</s:SegmentedControl>

<Label Text="Example with SegmentedControlOptions defined in XAML. Events subscribed in code-behind.&#10;No styles applied. WidthRequest=160." />

<s:SegmentedControl
x:Name="SegmentedControl"
Expand Down
9 changes: 8 additions & 1 deletion Samples/SegmentedControlDemoApp/Views/Test3Page.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@
Spacing="20"
VerticalOptions="Start">

<Label Text="Example with data binding complex item view models to ItemsSource and SegmentedItem" />
<Label Text="Example with data binding complex item view models to ItemsSource and SelectedItem" />

<s:SegmentedControl
ItemsSource="{Binding Countries}"
SelectedItem="{Binding SelectedCountry, Mode=TwoWay}"
TextPropertyName="EnglishName" />

<Label Text="Example with data binding complex item view models but leaving the TextPropertyName empty." />

<s:SegmentedControl
ItemsSource="{Binding Countries}"
SelectedItem="{Binding SelectedCountry, Mode=TwoWay}"
TextPropertyName="{x:Null}" />

<Grid BackgroundColor="{StaticResource Gray100}">
<Label Text="{Binding SelectedCountry.OfficialName, StringFormat='{}SelectedCountry: {0}'}" />
</Grid>
Expand Down

0 comments on commit a82864a

Please sign in to comment.