UWP 中的 ListView 控件是否有 IsSelected 属性
Do the ListView Control have IsSelected Property in UWP
如果是,我应该如何将可见性绑定到 ListViewItem 的 IsSelected 属性?
The ListViewItem class provides the container for items displayed in a ListView control. You populate the ListView by adding objects directly to its Items collection or by binding its ItemsSource property to a data source. When items are added to the ListView, a ListViewItem container is created automatically for each item in the collection.
更多信息,请参考Remark of the ListViewItem。
我们应该能够使用 SelectionChangedEventArgs.AddedItems
获取包含所选项目的列表。我们还可以使用 SelectionChangedEventArgs.RemovedItems
获取包含未选择的项目的列表。
我们可以在Class中定义IsSelected
属性,Class应该继承INotifyPropertyChanged
。在SelectionChanged
事件中,我们可以将Selected Item和UnSelect Item设置为Item的IsSelected属性
例如:
public class Item : INotifyPropertyChanged
{
private string _title;
private bool _isSelected;
public string Title
{
get { return _title; }
set
{
_title = value;
RaisePropertyChanged("Title");
}
}
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
RaisePropertyChanged("IsSelected");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
然后我们可以在不转换类型的情况下将Visibility
绑定到IsSelected
。
例如:
<ListView Name="MyListView" SelectionChanged="MyListView_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" Visibility="{Binding IsSelected, Mode=OneWay}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
背后的代码:
private ObservableCollection<Item> Items;
public MainPage()
{
this.InitializeComponent();
Items = new ObservableCollection<Item>();
Items.Add(new Item());
Items[0].Title = "Title1";
Items.Add(new Item());
Items[1].Title = "Title2";
MyListView.ItemsSource = Items;
}
private void MyListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (Item item in e.RemovedItems)
{
item.IsSelected = false;
}
foreach (Item item in e.AddedItems)
{
item.IsSelected = true;
}
}
如果是,我应该如何将可见性绑定到 ListViewItem 的 IsSelected 属性?
The ListViewItem class provides the container for items displayed in a ListView control. You populate the ListView by adding objects directly to its Items collection or by binding its ItemsSource property to a data source. When items are added to the ListView, a ListViewItem container is created automatically for each item in the collection.
更多信息,请参考Remark of the ListViewItem。
我们应该能够使用 SelectionChangedEventArgs.AddedItems
获取包含所选项目的列表。我们还可以使用 SelectionChangedEventArgs.RemovedItems
获取包含未选择的项目的列表。
我们可以在Class中定义IsSelected
属性,Class应该继承INotifyPropertyChanged
。在SelectionChanged
事件中,我们可以将Selected Item和UnSelect Item设置为Item的IsSelected属性
例如:
public class Item : INotifyPropertyChanged
{
private string _title;
private bool _isSelected;
public string Title
{
get { return _title; }
set
{
_title = value;
RaisePropertyChanged("Title");
}
}
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
RaisePropertyChanged("IsSelected");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
然后我们可以在不转换类型的情况下将Visibility
绑定到IsSelected
。
例如:
<ListView Name="MyListView" SelectionChanged="MyListView_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" Visibility="{Binding IsSelected, Mode=OneWay}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
背后的代码:
private ObservableCollection<Item> Items;
public MainPage()
{
this.InitializeComponent();
Items = new ObservableCollection<Item>();
Items.Add(new Item());
Items[0].Title = "Title1";
Items.Add(new Item());
Items[1].Title = "Title2";
MyListView.ItemsSource = Items;
}
private void MyListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (Item item in e.RemovedItems)
{
item.IsSelected = false;
}
foreach (Item item in e.AddedItems)
{
item.IsSelected = true;
}
}