Edude15000
Edude15000

Reputation: 81

How to call PropertyChanged on Label for other property

Hello I was curious if it was possible to set PropertyChanged for a different property? My Label is bound to Bandmember and the text is bound to a property called FormattedName.

As this sits right now, it will only run this PropertyChanged event when the FormattedName property is changed. I have a different property called Happiness on Bandmember that I want this to call the PropertyChanged event for when Happiness is updated not FormattedName.

XAML:

<ListView x:Name="dayView" ItemsSource="{Binding BandMembers}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Label Grid.Column="0" x:Name="dayViewFormattedNameLabel" FontSize="Small" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"
                                       Text="{Binding FormattedName}" PropertyChanged="DayViewFormattedNameLabel_PropertyChanged" />
                                <Picker Grid.Column="1" FontSize="Small" Title="{Binding FormattedName, StringFormat='Select Task For {0}'}" x:Name="TaskPickerInListView" 
                                       ItemsSource="{Binding AvailableTasks}" SelectedItem="{Binding AssignedTask}" ItemDisplayBinding="{Binding TaskDescription}" 
                                       SelectedIndexChanged="TaskPickerUpdated" />
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

Thanks!

Upvotes: 0

Views: 223

Answers (1)

jgoldberger - MSFT
jgoldberger - MSFT

Reputation: 6088

Ah, found your new question (I see you realized why it was not working before)

I suggest using a value converter instead.

What this will do is convert the int value of Happiness to a color, and you can then bind the Label's TextColor property to Happiness using an IntToColorConverter, e.g.:

public class IntToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double percent = (double)((int)value) / 100;
        double resultRed = Color.Red.R + percent * (Color.Green.R - Color.Red.R);
        double resultGreen = Color.Red.G + percent * (Color.Green.G - Color.Red.G);
        double resultBlue = Color.Red.B + percent * (Color.Green.B - Color.Red.B);
        return new Color(resultRed, resultGreen, resultBlue);
    }

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

Then to use it in XAML:

<ContentPage.Resources>
    <ResourceDictionary>
        <local:IntToColorConverter x:Key="intToColor" />
    </ResourceDictionary>
</ContentPage.Resources>

<Label ... 
     TextColor="{Binding Happiness, Converter={StaticResource intToColor}}">

Upvotes: 2

Related Questions