Home > Net >  how to detect text value change for label element in xamarin UWP application?
how to detect text value change for label element in xamarin UWP application?

Time:10-27

I need a way to detect when a specific label element has it's text value set. This happens programmatically at the start when the application is loading. I query sharepoint, and grab a list of files. For each file, I display whether it's checked out or not.

If the file is checked out, I then want to search the LOCAL computer for a custom token file that determines if it's the current user that has it checked out or not. If I find a corresponding token for the specific file on the local computer, I want to change the color of the text / icon that I'm currently showing to red.

Problem

At this point, the initial list of files shows properly, and ... the logic that displays a specific icon when the DriveItem.Publication.Level is checkout works fine. But in my new event handler that tries to detect when the checkout icon is set, I don't know how to: a) only check for changes to the specific label. b) grab the name of the associated file (another label element) c) programmatically change the color of the icon.

So far, I have the following code:

XAML

Based on my research, I understand that since the label doesn't have a textchanged event i have to add an event handler for the entire class. Notice the "PropertyChanged="SPDocumentLibraryContentsChanged" below:

 <?xml version="1.0" encoding="utf-8" ?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:GraphTutorial.Models"

     Title="Shared Document Library"
     x:Class="GraphTutorial.SPDocumentLibraryContentsPage"
         PropertyChanged="SPDocumentLibraryContentsChanged">

And the later on I have the following elements. It's the checkout status that I need to evaluate and change the color for, if it's set to "checkout"

 <Label Grid.Column="0" Text="{Binding Path=DriveItem.Name}" FontSize="Small" />
 <Label Grid.Column="1" x:Name="CheckoutStatus" Text="{Binding Path=DriveItem.Publication.Level,Converter={StaticResource IconValueConverter}}" FontFamily="Segoe MDL2 Assets" FontSize="Small"/>

CS Code

This is where my problem is. first I can't seem to capture the right propertyname. I've tried

  • DriveItem.Publication.Level
  • Level

But neither seems to work.

    private void SPDocumentLibraryContentsChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "Level")
        {
            Console.WriteLine(sender);
            Console.Write(e);
        }

The example I was trying to follow is this: Handle event when Label Text Change

Any tips would be appreciated.

CodePudding user response:

how to detect text value change for label element in xamarin UWP application?

Label contains PropertyChanged event, you could listen it for checking if current Text property changed.

For example

MyLabel.PropertyChanged  = MyLabel_PropertyChanged;
private void MyLabel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Text")
    {
        // process logic
    }
}

And if you want to listen the binding property from view model, you need add event handler for your viewmodel. For more please refer to this case reply

For example

public MyClass() => PropertyChanged  = MyClass_PropertyChanged;
private void MyClass_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    if (e.PropertyName == nameof(LocationName) || e.PropertyName == nameof(SubLocationName))
    {
        RaisePropertyChanged(nameof(LocationCompleteString));
    }
}
  • Related