2017-08-28 58 views
0

我有一个类ImageButton:Grid并且想要检测其变化IsEnabled属性。有没有一种基于事件的方式来做到这一点?如何检测对视觉元素内置属性的更改? - Xamarin Forms

PropertyChanged事件在这里看起来并不相关。

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

背景:我的课实现了一个可点击的图像覆盖文本,作为一个按钮。这是一个单格栅格,上面覆盖了一个图像,上面覆盖了一个标签。当ImageButton对象被禁用时,我需要减少标签和图像的不透明度。当然,我可以简单地添加一个属性来做到这一点,但是无法轻松地将该类用作插入到使用Button的现有代码的插件。

备注:Button不提供BackgroundImage属性有点让人困惑 - 许多开发人员都需要这样做。

+0

我相信,除非我明白你要找什么不好因为,PropertyChanged实际上就是你所需要的,因为PropertyChangedEventArgs提供了你已经改变的属性的名称:https://developer.xamarin.com/api/type/System.ComponentModel.PropertyChangedEventArgs/ – Kinxil

+0

可能相关的https:/ /forums.xamarin.com/discussion/20040/disabled-button-style ...他们使用'PropertyChanged'事件和一个额外的风格,以完成非常相似的事情...检查NinoStella的帖子。 – Hackerman

回答

1

您可以在家长控制简单覆盖OnPropertyChanged更新内部控制(S):

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

我使用了绑定方法@Sharada Gururaj。完美的作品,非常感谢! – BillF