ตั้งค่าพื้นหลังสำหรับรายการที่เลือกใน listview

ฉันมี 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; }

person user1884032    schedule 03.01.2013    source แหล่งที่มา
comment
ดูที่ stackoverflow.com/questions/6171502/ คำตอบของ Gishu สร้างคุณสมบัติหรือฟังก์ชันที่สลับตาม IsSelected เพื่อผูกเป็นสี   -  person JDwyer    schedule 03.01.2013
comment
คุณควรใช้โปรแกรมแปลงสีสำหรับการเปลี่ยนสีของรายการที่เลือก   -  person Jignesh.Raj    schedule 03.01.2013


คำตอบ (1)


คุณสามารถตั้งค่า DataTrigger เป็นคุณสมบัติ CarIsSelected ได้ แต่คุณจะต้องใช้ INotifyPropertyChanged ในคลาส CarsInfo ของคุณเพื่อให้ UI สะท้อนถึงการเปลี่ยนแปลง

xaml:

    <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>

code

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));
        }
    }
}

ผลลัพธ์: ป้อนคำอธิบายรูปภาพที่นี่

person sa_ddam213    schedule 03.01.2013
comment
โอ้พระเจ้า. ฉันใช้เวลากับเรื่องนี้มาประมาณสองสามชั่วโมงแล้วและสิ่งที่คุณแนะนำก็ใช้ได้ผล ฉันกำลังทำ datatrigger บน listviewitem แทนที่จะเป็นป้ายกำกับ สิ่งที่คุณแนะนำนั้นสมเหตุสมผล ขอขอบคุณอีกครั้ง. - person user1884032; 03.01.2013