[英]Displaying only selected items from ListBox in new List
I'm relatively fresh to WPF and discovering XAML. 我对WPF比较陌生,并发现XAML。
I've got one ListBox Name="EmployeeTitles"
in XAML (shown below). 我在XAML中有一个
ListBox Name="EmployeeTitles"
(如下所示)。 For the purpose of demonstrating the problem DataContext = employees
which is ObservableCollection<Employee>
. 为了展示的问题的目的
DataContext = employees
是ObservableCollection<Employee>
。
<ListBox Name="EmployeeTitles"
ItemsSource="{Binding}"
SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ToggleButton IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
<!--StackPanel will have more items-->
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}"/>
</StackPanel>
</ToggleButton>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My "Code Behind" class snippet looks as follows: 我的“代码隐藏”类代码段如下所示:
private string _title;
public string Title
{
get { return _title; }
set
{
_title = value;
OnPropertyChanged();
}
}
private bool _isSelected;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
OnPropertyChanged();
}
}
I would like to show the selected items in a different control. 我想在其他控件中显示所选项目。 For example have a list that shows only selected items.
例如,有一个仅显示选定项目的列表。 Can I somehow mark in XAML that I only only want to display items with
IsChecked="True"
? 我可以在XAML中以某种方式标记我只想显示
IsChecked="True"
吗? I know I could have another ObservableCollection storing only selected items and updating it in "Code Behind" whenever property IsSelected
changes, but that seems like an overhead and I suppose there should be a way to do it in XAML? 我知道我可以再有一个ObservableCollection,它仅存储选定的项,并在属性
IsSelected
发生更改时在“ IsSelected
代码”中对其进行更新,但这似乎是一项开销,我想应该有一种在XAML中做到这一点的方法吗?
You can bind the other listbox with the selecteditems of the original listbox. 您可以将其他列表框与原始列表框的选定项目绑定。 Try the below code.
试试下面的代码。
<Grid>
<StackPanel Orientation="Horizontal">
<ListBox x:Name="Emp" ItemsSource="{Binding EmpCollection}" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ToggleButton IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
<!--StackPanel will have more items-->
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}"/>
</StackPanel>
</ToggleButton>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox x:Name="SelectedEmp" ItemsSource="{Binding ElementName=Emp,Path=SelectedItems}" DisplayMemberPath="Title"/>
</StackPanel>
</Grid>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
class ViewModel
{
public ObservableCollection<Emp> EmpCollection { get; set; }
public ViewModel()
{
EmpCollection = new ObservableCollection<Emp>();
for (int i = 0; i < 10; i++)
{
EmpCollection.Add(new Emp() {Title = "Title"+i});
}
}
}
class Emp:INotifyPropertyChanged
{
private string _title;
public string Title
{
get { return _title; }
set
{
_title = value;
OnPropertyChanged();
}
}
private bool _isSelected;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.