如何在Label上为其他属性调用PropertyChanged

时间:2019-01-09 03:08:24

标签: c# xamarin binding label propertychanged

您好,我很好奇是否可以为其他属性设置PropertyChanged吗?我的标签绑定到Bandmember,文本绑定到名为FormattedName的属性。

现在,它PropertyChanged属性更改时,它将仅运行此FormattedName事件。我在Happiness上有一个名为Bandmember的属性,我希望它在PropertyChanged而不是Happiness更新时调用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>

谢谢!

1 个答案:

答案 0 :(得分:2)

啊,找到了您的新问题(我明白了您之前为什么不起作用)

我建议改用value converter

这将把“幸福”的int值转换为颜色,然后您可以使用LabelTextColor的{​​{1}}属性绑定到Happiness ,例如:

IntToColorConverter

然后在XAML中使用它:

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();
    }
}