databinding

3 posts, 1 answers
  1. Tony
    Tony avatar
    20 posts
    Member since:
    Dec 2011

    Posted 09 Nov 2018 Link to this post

    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. Answer
    Lance | Principal TSE
    Admin
    Lance | Principal TSE avatar
    1043 posts

    Posted 09 Nov 2018 Link to this post

    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
  3. Tony
    Tony avatar
    20 posts
    Member since:
    Dec 2011

    Posted 12 Nov 2018 Link to this post

    Thank you Lance, setting the Binding Mode to TwoWay fixed it.
Back to Top