ฉันมี listview ที่แสดงแถวผ่าน datatemplate เมื่อฉันเลือกรายการ ฉันจะเพิ่มรายการที่เลือกไปยังคอลเลกชันที่สังเกตได้ และยังตั้งค่าของ "CarIsSelected" ให้เป็นจริง เพื่อให้ฉันรู้ว่ารายการใดถูกเลือก ฉันประสบปัญหาในการตั้งค่าสีพื้นหลังสำหรับรายการที่ฉันเลือกซึ่งเพิ่มเข้าไปในคอลเลกชันที่สังเกตได้ ฉันหวังว่านี่จะสมเหตุสมผล ด้านล่างนี้คือรหัสของฉันที่ฉันมีจนถึงตอนนี้ อีกตัวอย่างหนึ่ง หากฉันเลือก "item1" ฉันจะเพิ่ม "item1" ลงในคอลเลกชันและไฮไลต์แถวสำหรับ "item1" จากนั้นฉันเลือก "item2" เพิ่ม "item2" ลงในคอลเลกชันและเน้นแถวสำหรับ "item2" ด้วย ดังนั้นควรเน้น "item1" และ "item2"
xaml:
<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; }