This is a migrated thread and some comments may be shown as answers.
databinding
2 Answers 22 Views
This is a migrated thread and some comments may be shown as answers.
Tony
Top achievements
Rank 1
Tony asked on 09 Nov 2018, 02:01 PM

I'm trying to databind to the SelectedIndex property of the Segmented control but its always returning 0

e.g.:

SelectedIndex="{Binding Item.ExpenseType}"

Where ExpenseType is defined as an int that has corresponding RadSegmentedControl.ItemsSource array, e.g.:

<input:RadSegmentedControl.ItemsSource>
                            <x:Array Type="{x:Type x:String}">
                                <x:String>Credit Card</x:String>
                                <x:String>Cash</x:String>                                
                            </x:Array>
</input:RadSegmentedControl.ItemsSource>

 

Can someone please confirm if this works?

2 Answers, 1 is accepted

Sort by
0
Accepted
Lance | Manager Technical Support
Telerik team
answered on 09 Nov 2018, 07:12 PM
Hello Tony,

Most Bindings use OneWay mode by default (there are some edge exceptions). This means the RadSlideView will never update the source. So if the default value of ExpenseType is 0, it will stay 0 even though RadSlideView.SelectedIndex has changed.

The fix this, use TwoWay binding:

SelectedIndex="{Binding Item.ExpenseType, Mode=TwoWay}"

Additionally, you want to make sure the ExpenseType property is using PropertyChanged

public class MyItem : INotifyPropertyChanged
{
    private int _expenseType;
 
    public int ExpenseType
    {
        get => _expenseType;
        set
        {
            if(_expenseType != value)
            {
                _expenseType = value;
                OnPropertyChanged();
            }
        }
    }
 
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}


Demo

Below is a full demo page confirm this works, however if you still experience a problem, please open a support ticket here and attach the rest of the code so that we can investigate directly (you have priority support).

<?xml version="1.0" encoding="utf-8" ?>
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:input="clr-namespace:Telerik.XamarinForms.Input;assembly=Telerik.XamarinForms.Input"
             xmlns:portable="clr-namespace:RealTimeFilteringDemos.Portable;assembly=RealTimeFilteringDemos.Portable"
             x:Class="TonyDemo.Portable.Page1">
 
    <ContentPage.BindingContext>
        <portable:Page1ViewModel />
    </ContentPage.BindingContext>
 
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="60" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
 
        <input:RadSegmentedControl x:Name="segmentControl"
                                   SelectedIndex="{Binding Item.ExpenseType, Mode=TwoWay}"
                                   HeightRequest="60">
            <input:RadSegmentedControl.ItemsSource>
                <x:Array Type="{x:Type x:String}">
                    <x:String>Credit Card</x:String>
                    <x:String>Cash</x:String>
                </x:Array>
            </input:RadSegmentedControl.ItemsSource>
        </input:RadSegmentedControl>
 
        <Label Text="{Binding Item.ExpenseType}"
               VerticalOptions="Center"
               HorizontalOptions="Center"
               Grid.Row="1" />
    </Grid>
</ContentPage>

namespace TonyDemo.Portable
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent();
        }
    }
 
    public class Page1ViewModel
    {
        public MyItem Item { get; set; } = new MyItem();
    }
 
    public class MyItem : INotifyPropertyChanged
    {
        private int _expenseType;
 
        public int ExpenseType
        {
            get => _expenseType;
            set
            {
                if (_expenseType != value)
                {
                    _expenseType = value;
                    OnPropertyChanged();
                }
            }
        }
 
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Runtime Result:




Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Tony
Top achievements
Rank 1
answered on 12 Nov 2018, 05:16 PM
Thank you Lance, setting the Binding Mode to TwoWay fixed it.
Asked by
Tony
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Tony
Top achievements
Rank 1
Share this question
or