C# 基于绑定的XAML帧背景更改

C# 基于绑定的XAML帧背景更改,c#,xaml,mvvm,C#,Xaml,Mvvm,我有一个基于以下类别的ObservableCollection: public class Data { public string Text { get; set; } public DateTime Date { get; set; } public bool IsActive { get; set; } } 此observablecollection作为ListView的ItemSource使用并绑定。以下是用于显示数据的数据模板- <DataTemplat

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

public class Data
{
    public string Text { get; set; }
    public DateTime Date { get; set; }
    public bool IsActive { get; set; }
}
此observablecollection作为ListView的ItemSource使用并绑定。以下是用于显示数据的数据模板-

<DataTemplate>
    <ViewCell>
        <Frame OutlineColor="White" HasShadow="False">
             <!-- Data -->
        </Frame>
    </ViewCell>
</DataTemplate>

不确定您的触发器问题,但我认为您应该能够通过首先在数据类上实现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));
        }
    }
}
然后,在xaml中,您应该能够执行以下操作:

<DataTemplate>
    <ViewCell>
        <Frame Background="{Binding IsActive, Converter={StaticResource IsActiveToColorConverter}}" OutlineColor="White" HasShadow="False">
             <!-- Data -->
        </Frame>
    </ViewCell>
</DataTemplate>

OP并没有提到布尔属性将被实时更改。所以notify属性不是必需的。但对于这种情况,转换器是一个很好的选择。所以+1。您确定行号与定义框架样式的位置匹配吗?我不确定xamarin,但可能尝试使用{x:Type}扩展名,而不是依赖TargetType属性中的字符串->类型转换?是否直接在
标记中设置
背景颜色?如果是,该属性将覆盖任何触发的或样式化的值。另一种尝试是可能使用
TargetType={x:Type Frame}
,有时我会看到文本字符串无法解析为实际类型,因此这可以防止出现这种情况。
Xamarin.Forms.Xaml.XamlParseException: Position 28:26. The Property TargetType is required to create a Xamarin.Forms.DataTrigger object.
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));
        }
    }
}
<DataTemplate>
    <ViewCell>
        <Frame Background="{Binding IsActive, Converter={StaticResource IsActiveToColorConverter}}" OutlineColor="White" HasShadow="False">
             <!-- Data -->
        </Frame>
    </ViewCell>
</DataTemplate>
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();
    }
}