在列表视图中选择项目组后台

编程入门 行业动态 更新时间:2024-10-19 22:31:44
本文介绍了在列表视图中选择项目组后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有过一个DataTemplate显示行列表视图。当我选择一个项目,我所选择的项目添加到一个观察的集合,也可以设置CarIsSelected的值设置为true,所以我知道它被选中。我有我如何设置为添加到的ObservableCollection我选择项目的背景颜色烦恼吗?我希望这是有道理的。下面是我的code,我有这么远。再举一个例子,如果我选择ITEM1,我加上ITEM1的收集和突出显示该行的ITEM1。然后,我选择ITEM2,加上ITEM2的收集,也突出了ITEM2的行。因此,ITEM1和ITEM2应该被突出显示。

XAML:

< ListView控件名称=lstExistingProblemList的Horizo​​ntalAlignment =左VerticalAlignment =底          的ItemsSource ={绑定路径= ListOfCars}的SelectedItem ={结合SelectedCar}          的SelectionMode =单WIDTH =391Grid.ColumnSpan =2>    < ListView.ItemTemplate>        <&DataTemplate的GT;            <标签宽度=391保证金=0,0,5,0的Horizo​​ntalAlignment =左>                < Label.Content>                    <&的TextBlock GT;                        < TextBlock.Text>                            < MultiBinding的StringFormat ={} {0} - {1}>                                <绑定路径=做/>                                <绑定路径=模式/>                             < / MultiBinding>                        < /TextBlock.Text>                    < / TextBlock的>                < /Label.Content>            < /标签>        < / DataTemplate中>    < /ListView.ItemTemplate>< /&的ListView GT;

视图模型:

的ObservableCollection< CarsInfo> selectedCarsLists =新的ObservableCollection< CarsInfo>();清单< CarsInfo> listOfCars =新的List< CarsInfo>();CarsInfo selectedCar;公开名单< CarsInfo> ListOfCars{    得到    {        listOfCars = DataSource.GetCarInfo();        返回listOfCars;    }}公众的ObservableCollection< CarsInfo> SelectedCarsLists{    得到    {        返回selectedCarsLists;    }}公共CarsInfo SelectedCar{    得到    {        返回selectedCar;    }    组    {        如果(selectedCar!=值)        {            selectedCar =价值;            selectedCar.CarIsSelected = TRUE;            selectedCarsLists.Add(selectedCar);        }    }}

类:

公众诠释新年{搞定;组; }公共字符串描述{搞定;组; }公共字符串制作{搞定;组; }公共字符串模式{搞定;组; }公共BOOL CarIsSelected {搞定;组; }

解决方案

您可以设置一个 DataTrigger 到 CarIsSelected 属性,但你将不得不实施 INotifyPropertyChanged的为UI以反映更改。

XAML:

< ListView.ItemTemplate>        <&DataTemplate的GT;            <标号x:名称=标签WIDTH =391保证金=0,0,5,0的Horizo​​ntalAlignment =左>                < Label.Content>                    <&的TextBlock GT;                    < TextBlock.Text>                        < MultiBinding的StringFormat ={} {0} - {1}>                            <绑定路径=做/>                            <绑定路径=模式/>                         < / MultiBinding>                    < /TextBlock.Text>                    < / TextBlock的>                < /Label.Content>            < /标签>            < D​​ataTemplate.Triggers>                < D​​ataTrigger绑定={结合CarIsSelected}VALUE =真>                    <二传手的TargetName =标签属性=背景VALUE =红/>                < / DataTrigger>            < /DataTemplate.Triggers>        < / DataTemplate中>    < /ListView.ItemTemplate>< /&的ListView GT;

code

公共类CarsInfo:INotifyPropertyChanged的{    公众诠释年度{搞定;组; }    公共字符串描述{搞定;组; }    公共字符串制作{搞定;组; }    公共字符串模式{搞定;组; }    私人布尔_carIsSelected;    公共BOOL CarIsSelected    {        {返回_carIsSelected; }        集合{_carIsSelected =价值; NotifyPropertyChanged(CarIsSelected); }    }    公共事件PropertyChangedEventHandler的PropertyChanged;    私人无效NotifyPropertyChanged(字符串属性)    {        如果(的PropertyChanged!= NULL)        {            的PropertyChanged(这一点,新PropertyChangedEventArgs(属性));        }    }}

结果:

I have a listview which display the row through a datatemplate. When I select an item, I add the selected item to an observable collection and also set the value of "CarIsSelected" to true so I know which is selected. I'm having trouble on how I set the background color for the items I've selected that are added to the observablecollection? I hope this makes sense. Below is my code that I have so far. To give another example, if I select "item1", I add "item1" to the collection and highlight the row for "item1". I then select "item2", add "item2" to the collection and also highlight the row for "item2". Therefore "item1" and "item2" should be highlighted.

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>

ViewModel:

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

class:

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

解决方案

You can setup a DataTrigger to the CarIsSelected property, but you will have to implement INotifyPropertyChanged in your CarsInfo class for the UI to reflect the changes.

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

Result:

更多推荐

在列表视图中选择项目组后台

本文发布于:2023-11-10 22:19:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1576613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   后台   项目   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!