0

首先让我说我是新手MVVM,所以请坦率地说,如果问题不清楚,请告诉我,我会尽力澄清。

我有一个成功绑定 ListView 的按钮。(它填充列表视图)。下面是按钮VM代码:

<Button Content="Fetch Data" Command="{Binding readFilesCommand}" CommandParameter="{Binding Path=Text, ElementName=browseFolderTextBox}" Name="button1" />

正在填充的 listView 如下所示:

<ListView SelectionMode="Extended" Name="responseListView" ItemsSource="{Binding}" GridViewColumnHeader.Click="responseListViewClick" >
    <ListView.Resources>
        <local:IndexConverter x:Key="IndexConverter" />
        <DataTemplate x:Key="OrdinalColumnDataTemplate">
            <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem},
                Converter={StaticResource ResourceKey=IndexConverter}}" HorizontalAlignment="Right" />
        </DataTemplate>
    </ListView.Resources>
    <ListView.View>
        <GridView x:Name="gridView2" AllowsColumnReorder="True">
            <GridViewColumn Width="28" Header="#" CellTemplate="{StaticResource ResourceKey=OrdinalColumnDataTemplate}" />
            <GridViewColumn Width="80" DisplayMemberBinding="{Binding Name}" Header="Name" />
            <GridViewColumn Width="150" DisplayMemberBinding="{Binding EMail}" Header="EMail" />
            <GridViewColumn Width="75" DisplayMemberBinding="{Binding Date}" Header="Date" />
            <GridViewColumn Width="75" DisplayMemberBinding="{Binding Time}" Header="Time" />
        </GridView>
    </ListView.View>
</ListView>

下面是在 listView 中填充的类的代码。

public class ResourceList : ObservableCollection<Resource>
{
    public ResourceList() : base()
    {
    }
}

public class Resource : INotifyPropertyChanged
{
    public Resource()
    {
        Name = "";
        EMail = "";
        Date = "";
        Time = "";
        SWList = new ObservableCollection<string>();
    }

    private string name;
    private string eMail;
    private string time;
    private string date;

    public string Name
    {
        get { return name;}
        set 
        {
            if(name != value) 
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    public string EMail
    {
        get { return eMail; }
        set 
        {
            if (eMail != value) 
            {
                eMail = value;
                OnPropertyChanged("EMail");
            }
        }
    }

    public string Date
    {
        get { return date;}
        set 
        {
            if (date != value) 
            {
                date = value;
                OnPropertyChanged("Date");
            }
        }
    }

    public string Time
    {
        get { return time; }
        set 
        {
            if (time != value) 
            {
                time = value;
                OnPropertyChanged("Time");
            }
        }
    }

    // This interface causes the View to be notified of changes to the instances of Resource.
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if(handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public ObservableCollection<string> SWList { get; set; }
}

// ObservableCollection notifies the View of changes to the collection (add, delete, move items)
public class Licenses : ObservableCollection<Licenses>
{
    public Licenses()
    {
    }
    public string Name { get; set; }
    public string License { get; set; }
}

到目前为止一切正常。现在谈谈我的问题。我希望 ListView 的每一行都有背景颜色。假设某一行缺少属性时间,那么我希望整行都是红色的。我应该从哪里开始?

4

1 回答 1

0

ListViewItem您可以用一个容器包围每个容器(使用模板执行此操作)并使用绑定或 DataTrigger 更改此容器的背景颜色。

此处的一个示例:更改 ListView 中 GridView 行的背景颜色

编辑:根据 MVVM,您应该在您的 class 上定义一个业务属性Resource,其类型为boolean(IsValid ?)或 enum(Status ?),并在 Binding 中使用转换器将值转换为SolidColorBrush(例如)。

于 2013-10-22T11:45:53.050 回答