基于绑定的XAML更改框架背景

Ove*_*ack 4 c# xaml mvvm

我有一个基于以下类的ObservableCollection:

public class Data
{
    public string Text { get; set; }
    public DateTime Date { get; set; }
    public bool IsActive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

使用此observablecollection并将其绑定为ListView的ItemSource。以下是用于显示数据的DataTemplate-

<DataTemplate>
    <ViewCell>
        <Frame OutlineColor="White" HasShadow="False">
             <!-- Data -->
        </Frame>
    </ViewCell>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

由于ObservableCollection是具有boolean属性的Data类的集合,因此我想使用它来更改框架的背景色

  • 如果属性IsActive为true-BackgroundColor为Red

  • 如果属性IsActive为false-BackgroundColor为蓝色

我已经研究了触发器的实现,但是似乎无法使其正常工作,并且不确定所缺少的内容。

根据Xamarin文档,我应该能够:

<Frame>
    <Frame.Trigger>
        <!-- -->
    </Frame.Trigger>  
</Frame>
Run Code Online (Sandbox Code Playgroud)

但是,这似乎是不可能的。这也不是-

<Frame>
<Frame.Style>
    <Style TargetType="Frame">
        <Setter Property="BackgroundColor" Value="Blue" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsActive}" Value="True">
                <Setter Property="BackgroundColor" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Frame.Style>
</Frame>
Run Code Online (Sandbox Code Playgroud)

上面的代码给出以下错误消息:

Xamarin.Forms.Xaml.XamlParseException: Position 28:26. The Property TargetType is required to create a Xamarin.Forms.DataTrigger object.
Run Code Online (Sandbox Code Playgroud)

Jac*_*ley 5

不确定触发问题,但我认为您应该能够通过首先在Data类上实现INotifyPropertyChanged来完成颜色更改,如下所示:

public class Data : INotifyPropertyChanged
{
    public string Text { get; set; }
    public DateTime Date { get; set; }

    private bool _isActive;
    public bool IsActive 
    {
        get { return _isActive; }
        set
        {
            if (value == _isActive)
            {
                return;
            }

            _isActive = value;

            NotifyPropertyChanged("IsActive");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,在您的xaml中,您应该可以执行以下操作:

<DataTemplate>
    <ViewCell>
        <Frame Background="{Binding IsActive, Converter={StaticResource IsActiveToColorConverter}}" OutlineColor="White" HasShadow="False">
             <!-- Data -->
        </Frame>
    </ViewCell>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

IsActiveToColorConverter的位置类似于:

public class IsActiveToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var isActive = (bool) value;

        return isActive ? "Red" : "Blue";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)