【发布时间】:2016-01-08 06:43:53
【问题描述】:
我正在使用 wpf 中的列表框。我的问题是我只从列表框中获取一个选定的项目。我想获取所有选定但不知道如何实现的项目?请帮忙! 这是我的代码:
<ListBox ItemsSource="{Binding Markets}" BorderThickness="0" Background="{x:Null}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionMode="Multiple" SelectedItem="{Binding SelectedItem}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}" Style="{DynamicResource CheckBoxStyle1}" Width="140" Margin="10"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我的视图模型内容:
private string _selectedItem;
public List<string> Markets { get; set;}
public SettingVM()
{
Markets = new List<string>();
Markets.Add("United Kingdom");
Markets.Add("Australia");
}
public string SelectedItem
{
get
{
return _selectedItem;
}
set
{
Set(() => SelectedItem, ref _selectedItem, value);
}
}
【问题讨论】:
-
您可以使用 ListBox.SelectionMode="Multiple" 和 ListBox.SelectedItems 来访问选定的项目。
-
我不知道,但你可以试试这个:littlebigtomatoes.com/2013/02/…
-
如何使用或绑定 Listbox.SelectedItems ? @tgpdyk
-
ListBox.SelectedItems是只读的,所以你不能设置Binding。您应该选择自己的自定义AttachedProperty。 -
我只是想获取项目字符串,那么如何实现呢? @AnjumSKhan
标签: c# .net wpf listbox viewmodel