0

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.

dot
  • 14,928
  • 41
  • 110
  • 218
  • why do you need to monitor the UI for a change? Your code is updating the UI, so why don't you just monitor for the change at the source? – Jason Oct 25 '21 at 15:03
  • When the users launch my app, the only info that's available from the main upstream database is - the list of shared files, and whether the file is checked out or not. It doesn't store who the file is checked out to. So for now, when a user checks out the file using my app - i create a hidden file locally. when they check in, I delete the hidden file. On application start up, I need to a) grab the list from upstream. b) force the app to check my local folder for hidden files and then update the color of the icons. – dot Oct 25 '21 at 15:09
  • that still doesn't explain why you are relying on the UI to detect changes in the state of your data. – Jason Oct 25 '21 at 15:11
  • @Jason I guess for the application start up scenario, I can call a "checklocalfolder()" method of sorts, and call it after I initialize the list... but i wnated to avoid having to check the contents of the entire shared file list each time. So ... for app start up, since the GUI is already looping through and filling in the status for each file - i though it would be more efficient to track when the text property changes – dot Oct 25 '21 at 15:13
  • it sounds like you are missing an internal data model that drives your UI – Jason Oct 25 '21 at 15:18
  • You shouldn't be monitoring the UI (including code behind files (view.xaml.cs) or using the UI to perform your start-up logic. It is for interaction with the user only. The UI should be bound to ViewModel properties using the ViewModel as the View's datacontext. The ViewModel properties are set programmatically using logic that either resides within the ViewModel or (better yet) is passed as a Model class from App code. Your code should be structured such that you can remove the View altogether and imitate a user by driving it programmatically through the ViewModel e.g. when unit testing. – ChrisBD Oct 25 '21 at 16:02
  • kk thanks guys. this is my first foray into windows development, xaml and c#. But I'll do some more reading. thanks. – dot Oct 25 '21 at 17:13

1 Answers1

0

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));
    }
}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36