Стиль Trigger работает только в одном направлении, когда привязан к IsEnabled пользовательского класса - PullRequest
1 голос
/ 07 марта 2019

Я сделал пользовательский TimePicker и пытаюсь отключить его с самого начала, и включаю его только тогда, когда что-то было сделано первым. Чтобы он отображался как отключенный, я использую триггер стиля, чтобы изменить его внешний вид, связанный со свойством IsEnabled.

Моя проблема заключается в том, что этот триггер работал только при первой инициализации, поэтому стиль выглядит как отключенный. но как только я включу его, триггер не изменит внешний вид обратно. Чего мне не хватает?

мой триггер

<Style TargetType="customControlls:BorderedTimePicker">
<Style.Triggers>
    <Trigger TargetType="customControlls:BorderedTimePicker" Property="IsEnabled" Value="False">
        <Setter Property="BorderColor" Value="{StaticResource varColBgGrey}" />
        <Setter Property="TextColor" Value="{StaticResource varColBgGrey}" />
        <Setter Property="Image" Value="ClockDisabled.png" />
    </Trigger>
</Style.Triggers>

Для большей ясности, я уверен, что IsEnabled изменился, поскольку TimePicker работает после того, как для параметра установлено значение true, но внешний вид по-прежнему не меняется.

Мой пользовательский TimePicker:

открытый класс BorderedTimePicker: TimePicker { #region Bindables

public static readonly BindableProperty MinimumTimeProperty = BindableProperty.Create(nameof(MinimumTime), typeof(DateTime?), typeof(BorderedTimePicker), null);

public DateTime MinimumTime
{
    get => (DateTime)GetValue(MinimumTimeProperty);
    set => SetValue(MinimumTimeProperty, value);
}

public static readonly BindableProperty MaximumTimeProperty = BindableProperty.Create(nameof(MaximumTime), typeof(DateTime?), typeof(BorderedTimePicker), null);

public DateTime MaximumTime
{
    get => (DateTime)GetValue(MaximumTimeProperty);
    set => SetValue(MaximumTimeProperty, value);
}

public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(nameof(BorderColor), typeof(Color), typeof(BorderedTimePicker), Color.DarkGray, BindingMode.TwoWay);

public Color BorderColor
{
    get => (Color)GetValue(BorderColorProperty);
    set
    {
        SetValue(BorderColorProperty, value);
        OnPropertyChanged();
    }
}

public static readonly BindableProperty TextSizeProperty = BindableProperty.Create(nameof(TextSize), typeof(int), typeof(BorderedTimePicker), 16);

public int TextSize
{
    get => (int)GetValue(TextSizeProperty);
    set => SetValue(TextSizeProperty, value);
}

public static readonly BindableProperty BorderThicknessProperty = BindableProperty.Create(nameof(BorderThickness), typeof(double), typeof(BorderedTimePicker), 2.0);

public double BorderThickness
{
    get => (double)GetValue(BorderThicknessProperty);
    set => SetValue(BorderThicknessProperty, value);
}

public static readonly BindableProperty BorderRadiusProperty = BindableProperty.Create(nameof(BorderRadius), typeof(double), typeof(BorderedTimePicker), 20.0);

public double BorderRadius
{
    get => (double)GetValue(BorderRadiusProperty);
    set => SetValue(BorderRadiusProperty, value);
}

public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create(nameof(Placeholder), typeof(string), typeof(BorderedTimePicker), string.Empty);

public string Placeholder
{
    get => (string)GetValue(PlaceholderProperty);
    set => SetValue(PlaceholderProperty, value);
}

public static readonly BindableProperty ImageProperty = BindableProperty.Create(nameof(Image), typeof(string), typeof(BorderedTimePicker), string.Empty, BindingMode.TwoWay);

public string Image
{
    get => (string)GetValue(ImageProperty);
    set
    {
        SetValue(ImageProperty, value);
        OnPropertyChanged();
    }
}

public static readonly BindableProperty ImageSizeProperty = BindableProperty.Create(nameof(ImageSize), typeof(int), typeof(BorderedTimePicker), 35);

public int ImageSize
{
    get => (int)GetValue(ImageSizeProperty);
    set => SetValue(ImageSizeProperty, value);
}

public static readonly BindableProperty IsValidProperty = BindableProperty.Create(nameof(IsValid), typeof(bool), typeof(BorderedDatePicker), true);

public bool IsValid
{
    get => (bool)GetValue(IsValidProperty);
    set
    {
        SetValue(IsValidProperty, value);
        OnPropertyChanged();
    }
}

public static readonly BindableProperty NullableDateProperty = BindableProperty.Create(nameof(NullableDate), typeof(DateTime?), typeof(ExtendedDatePicker), null, BindingMode.TwoWay);

public DateTime? NullableDate
{
    get => (DateTime?)GetValue(NullableDateProperty);
    set
    {
        if (value != NullableDate)
        {
            SetValue(NullableDateProperty, value);
            UpdateDate();
        }
    }
}

#endregion Bindables

public BorderedTimePicker()
{
}

private void UpdateDate()
{
    if (NullableDate.HasValue)
    {
        Time = NullableDate.Value.TimeOfDay;
    }
}

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

    if (propertyName == TimeProperty.PropertyName)
    {
        if (NullableDate != null) NullableDate = NullableDate.Value + Time;
    }
}

РЕДАКТИРОВАТЬ: соответствующая часть моего viewCell, где я использую триггер и таймер

<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:customControlls="clr-namespace:CleanKISeCare.CustomControlls;assembly=CleanKISeCare"
             xmlns:helpers="clr-namespace:Syncfusion.ListView.XForms.Helpers;assembly=Syncfusion.SfListView.XForms"
             xmlns:converter="clr-namespace:CleanKISeCare.Converter;assembly=CleanKISeCare"
             x:Class="CleanKISeCare.CustomControlls.DatePickerComponent" 
             x:Name="this">
             <ViewCell.BindingContext>
                  <helpers:InverseBoolConverter x:Key="InverseBoolConverter" />
              </ViewCell.BindingContext>
    <StackLayout x:Name="mainView" BindingContext="{Binding .}" BackgroundColor="White" Padding="10">
         <StackLayout.Resources>
                <ResourceDictionary>
                    <helpers:InverseBoolConverter x:Key="InverseBoolConverter" />
                    <converter:StringDateTimeConverter x:Key="StringDateTimeConverter" />
                    <Style TargetType="customControlls:BorderedTimePicker">
                        <Style.Triggers>
                            <Trigger TargetType="customControlls:BorderedTimePicker" Property="IsEnabled" Value="False">
                                <Setter Property="BorderColor" Value="{StaticResource varColBgGrey}" />
                                <Setter Property="TextColor" Value="{StaticResource varColBgGrey}" />
                                <Setter Property="Image" Value="ClockDisabled.png" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
            </ResourceDictionary>
</StackLayout.Resources>
            <Grid Margin="10"
                  HorizontalOptions="CenterAndExpand">
                <customControlls:BorderedTimePicker
                    Grid.Row="0" Grid.Column="1"
                    x:Name="timePicker"
                    Image="ClockPicker.png"
                    TextSize="14"
                    WidthRequest="160"
                    Placeholder="Zeit"
                    Margin="10,0,0,0"
                    HorizontalOptions="StartAndExpand"
                    MinimumTime="{Binding MinValue, Converter={StaticResource StringDateTimeConverter}}"
                    MaximumTime="{Binding MaxValue, Converter={StaticResource StringDateTimeConverter}}"
                    NullableDate="{Binding Answer, Source={x:Reference this}, Mode=TwoWay}"
                    BorderColor="{StaticResource varColLightDark}"
                    TextColor="{StaticResource varColDark}"
                    Format="HH:mm" />
            </Grid>
        </StackLayout>
</ViewCell>

и код:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DatePickerComponent : ViewCell
{
    public DatePickerComponent()
    {
        InitializeComponent();
        timePicker.IsEnabled = datePicker.NullableDate != null;
    }

    private static void OnPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
    {
        DatePickerComponent component = bindable as DatePickerComponent;
        component?.SetDate(newvalue);
    }

    void SetDate(object newvalue)
    {
        timePicker.IsEnabled = newDate.Value.Date != defaultDate;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...