【问题标题】:How to detect changes to a visual element's built-in properties? - Xamarin Forms如何检测视觉元素内置属性的变化? - Xamarin 形式
【发布时间】:2017-08-28 14:28:37
【问题描述】:

我有一个类 ImageButton: Grid 并想检测对其 IsEnabled 属性的更改。有没有基于事件的方法来做到这一点?

PropertyChanged 事件似乎与此处无关。

.NET 有 IsEnabledChanged,但这似乎也不适用。

背景:我的类实现了一个覆盖有文本的可点击图像,充当按钮。它是一个单单元格网格,其中包含一个覆盖有标签的图像。当 ImageButton 对象被禁用时,我需要降低标签和图像的不透明度。当然,我可以简单地添加一个属性来执行此操作,但不能轻松地将该类用作使用 Button 的现有代码的插件。

旁注:Button 不提供 BackgroundImage 属性有点令人费解 - 很多开发人员都需要它。

【问题讨论】:

标签: c# .net xamarin.forms inotifypropertychanged


【解决方案1】:

您可以简单地在父控件上覆盖 OnPropertyChanged 来更新内部控件:

protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    base.OnPropertyChanged(propertyName);

    if(propertyName == nameof(IsEnabled))
    {
         //update controls here
         ...
    }
}

但我宁愿建议您在将内部控件的Opacity 绑定到父级的IsEnabled 属性时使用转换器

例如,如果您在 C# 中定义了自定义控件,则可以将绑定定义为:

public class ImageButton : Grid
{
    private static readonly BooleanToOpacityConverter _converter = new BooleanToOpacityConverter();
    public ImageButton()
    {
        var label = new Label { Text = "ImageButton" };
        var image = new Image { Source = ImageSource.FromFile("icon.png") };

        // add binding to Opacity using IsEnabled from parent
        label.SetBinding(OpacityProperty, new Binding("IsEnabled", converter: _converter, source: this));
        image.SetBinding(OpacityProperty, new Binding("IsEnabled", converter: _converter, source: this));

        ColumnDefinitions = new ColumnDefinitionCollection { new ColumnDefinition(), new ColumnDefinition() };

        SetColumn(label, 1);
        Children.Add(label);
        Children.Add(image);
    }
}

或者,如果您使用基于 XAML 的自定义控件,您可以将绑定分配为:

<Grid xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:UpdateSourceTriggerApp"
    x:Name="_parent"
    x:Class="UpdateSourceTriggerApp.ImageButton2">
 <Grid.Resources>
   <ResourceDictionary>
     <local:BooleanToOpacityConverter x:Key="_converter" />
   </ResourceDictionary>
 </Grid.Resources>
 <Grid.ColumnDefinitions>
   <ColumnDefinition />
   <ColumnDefinition />
 </Grid.ColumnDefinitions>

 <Image Source="icon.png" Opacity="{Binding Source={x:Reference _parent}, Path=IsEnabled, Converter={StaticResource _converter}}" />
 <Label Text="ImageButton2" Grid.Column="1" Opacity="{Binding Source={x:Reference _parent}, Path=IsEnabled, Converter={StaticResource _converter}}" />
</Grid>

一个示例转换器如下所示:

public class BooleanToOpacityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var isEnabled = (value == null) ? false : (bool)value;
        return isEnabled ? 1 : 0.5;
    }

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

【讨论】:

  • 我使用了绑定方法@Sharada Gururaj。完美运行,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多