Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

On the listview switch based on a condition,tried a method but getting NotImplementedException?

SreeeeSreeee INMember ✭✭✭✭✭
edited October 2018 in Xamarin.Forms

I have a switch in my listview. I need to on the switch state if the switch item userid exists in the local DB. I tried like below:

In xaml, a converter added for the **IsToggled **property:

<Switch
          IsToggled="{Binding userProfileTO.userId, Converter={StaticResource isToggledConverter}}"
          HorizontalOptions="EndAndExpand"
          VerticalOptions="CenterAndExpand"/>

Converter code:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool toggle = false;
        string selectedIds = Application.Current.Properties["GroupUserIds"].ToString();
        if (!string.IsNullOrWhiteSpace(selectedIds))
        {
            List<int> TagIds = selectedIds.Split(',').Select(int.Parse).ToList();
            if (TagIds.Contains(Int32.Parse(value.ToString())))
            {
                toggle = true;
            }
            else
            {
                toggle = false;
            }
        }
        return toggle;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

But I am getting an exception on ConvertBack when running this code.

Exception thrown: 'System.NotImplementedException' in Myprojectname.dll
An exception of type 'System.NotImplementedException' occurred in Myprojectname.dll but was not handled in user code
The method or operation is not implemented.

Thanks in advance :)

Best Answer

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited October 2018 Accepted Answer

    Hi @ManojkumarMali :)
    The default binding type of IsToggled property is "Two-way". That's why my ConvertBack function is getting called. If I remove the throw new NotImplementedException(); in my ConvertBack method and everything will work fine. Or explicitly set the binding mode to be One-way.

    But this fix doesn't solve my real problem. :)My real problem is need to on the switch state if the switch item userid exists in the local DB.

Answers

  • ManojkumarMaliManojkumarMali USMember ✭✭✭

    Hi,

    Have you marked your converter as local resource on page under page resources?

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited October 2018 Accepted Answer

    Hi @ManojkumarMali :)
    The default binding type of IsToggled property is "Two-way". That's why my ConvertBack function is getting called. If I remove the throw new NotImplementedException(); in my ConvertBack method and everything will work fine. Or explicitly set the binding mode to be One-way.

    But this fix doesn't solve my real problem. :)My real problem is need to on the switch state if the switch item userid exists in the local DB.

  • SreeeeSreeee INMember ✭✭✭✭✭

    @Sreeee said:
    Hi @ManojkumarMali :)
    The default binding type of IsToggled property is "Two-way". That's why my ConvertBack function is getting called. If I remove the throw new NotImplementedException(); in my ConvertBack method and everything will work fine. Or explicitly set the binding mode to be One-way.

    But this fix doesn't solve my real problem. :)My real problem is need to on the switch state if the switch item userid exists in the local DB.

    Added more details here

Sign In or Register to comment.