0

我有一个列表视图,它通过数据模板显示行。当我选择一个项目时,我将所选项目添加到一个可观察的集合中,并将“CarIsSelected”的值设置为 true,以便我知道哪个被选中。我在如何为我选择的添加到 observablecollection 的项目设置背景颜色时遇到问题?我希望这是有道理的。以下是我到目前为止的代码。再举一个例子,如果我选择“item1”,我将“item1”添加到集合中并突出显示“item1”的行。然后我选择“item2”,将“item2”添加到集合中,并突出显示“item2”的行。因此应突出显示“item1”和“item2”。

xml:

<ListView Name="lstExistingProblemList" HorizontalAlignment="Left" VerticalAlignment="Bottom" 
          ItemsSource="{Binding Path=ListOfCars}" SelectedItem="{Binding SelectedCar}" 
          SelectionMode="Single" Width="391" Grid.ColumnSpan="2"> 

    <ListView.ItemTemplate>
        <DataTemplate>
            <Label Width="391" Margin="0,0,5,0" HorizontalAlignment="Left">
                <Label.Content>
                    <TextBlock>
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0}-{1}">
                                <Binding Path="Make" />
                                <Binding Path="Model" />
                             </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </Label.Content>
            </Label>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

视图模型:

ObservableCollection<CarsInfo> selectedCarsLists = new ObservableCollection<CarsInfo>();
List<CarsInfo> listOfCars = new List<CarsInfo>();
CarsInfo selectedCar;

public List<CarsInfo> ListOfCars
{
    get
    {       
        listOfCars = DataSource.GetCarInfo();
        return listOfCars;
    }
}

public ObservableCollection<CarsInfo> SelectedCarsLists
{
    get
    {
        return selectedCarsLists;
    }
}

public CarsInfo SelectedCar
{
    get
    {
        return selectedCar;
    }
    set
    {
        if (selectedCar != value)
        {
            selectedCar = value;
            selectedCar.CarIsSelected = true;
            selectedCarsLists.Add(selectedCar);
        }
    }
}

班级:

public int Year { get; set; }
public string Description { get; set; }
public string Make { get; set; } 
public string Model { get; set; }
public bool CarIsSelected { get; set; }
4

1 回答 1

1

DataTrigger您可以设置CarIsSelected属性,但您必须INotifyPropertyChanged在您的CarsInfo类中实现 UI 以反映更改。

xml:

    <ListView.ItemTemplate>
        <DataTemplate>
            <Label x:Name="label" Width="391" Margin="0,0,5,0" HorizontalAlignment="Left">
                <Label.Content>
                    <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0}-{1}">
                            <Binding Path="Make" />
                            <Binding Path="Model" />
                         </MultiBinding>
                    </TextBlock.Text>
                    </TextBlock>
                </Label.Content>
            </Label>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding CarIsSelected }" Value="True" >
                    <Setter TargetName="label" Property="Background" Value="Red" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

代码

public  class CarsInfo : INotifyPropertyChanged
{
    public int Year { get; set; }
    public string Description { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }

    private bool _carIsSelected;
    public bool CarIsSelected 
    {
        get { return _carIsSelected; }
        set { _carIsSelected = value; NotifyPropertyChanged("CarIsSelected"); } 
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

结果: 在此处输入图像描述

于 2013-01-03T03:30:06.367 回答