0

質問が不明確な場合は、私MVVMに知らせてください。明確にしようとします。

ListView を正常にバインドしているボタンがあります。(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 行の背景色を変更する

EDIT : MVVM によると、(IsValid ?) や enum (Status ?)Resourceなどのタイプの class でビジネス プロパティを定義し、Binding 内でコンバーターを使用して値を(たとえば) に変換する必要があります。booleanSolidColorBrush

于 2013-10-22T11:45:53.050 に答える