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();
}
}
我相信,除非我明白你要找什麼不好因爲,PropertyChanged實際上就是你所需要的,因爲PropertyChangedEventArgs提供了你已經改變的屬性的名稱:https://developer.xamarin.com/api/type/System.ComponentModel.PropertyChangedEventArgs/ – Kinxil
可能相關的https:/ /forums.xamarin.com/discussion/20040/disabled-button-style ...他們使用'PropertyChanged'事件和一個額外的風格,以完成非常相似的事情...檢查NinoStella的帖子。 – Hackerman