【发布时间】:2015-12-08 23:34:59
【问题描述】:
我正在尝试访问 SelectedItem 以及绑定到 ItemsControl 内嵌套 ListBox 的命令。下面是简化的 XAML。
<ItemsControl ItemsSource="{Binding Collection}"
ItemTemplate="{StaticResource Partials}"
</ItemsControl>
<DataTemplate x:Key="Partials">
<ListBox ItemsSource="{Binding ListBoxItems}"
SelectedItem="{Binding SelectedItem}" >
<ListBox.InputBindings>
<KeyBinding Key="Delete" Command="{Binding DeleteSelectedItemCommand/>
</ListBox.InputBindings>
</ListBox>
</DataTemplate>
这就是我在 VM 中所拥有的(简化版)
private void FillList()
{
//Populate the list
Collection.Add(Data);
var ListBoxViewSource = new CollectionViewSource { Source = ListBoxDataSource};
Collection.ListBoxItems= ListBoxViewSource.View;
}
private ObservableCollection<Scene> _collection= new ObservableCollection<Scene>();
public ObservableCollection<Scene> Collection
{
get { return _collection; }
set
{
if (value != _collection)
{
_collection= value; RaisePropertyChanged();
}
}
}
private string _selectedItem;
public string SelectedItem
{
get { return _selectedItem; }
set { _selectedItem= value; RaisePropertyChanged(); }
}
public RelayCommand DeleteSelectedItemCommand { get; private set; }
private void DeleteSelectedItem()
{
SourceData.Remove(SelectedItem);
}
如何获取列表框中SelectedItem的值?
【问题讨论】:
-
您是否在输出窗口中遇到任何绑定错误?
-
我认为没有必要像这样声明一个DataTemplate。
-
您的问题不清楚。请提供一个好的minimal reproducible example,它可以可靠地重现您遇到的任何问题,并准确描述该代码的作用以及您希望它做什么。询问如何使绑定发挥作用是没有帮助的,除非你确切地说出它们在“工作”时会做什么。
标签: c# wpf mvvm binding listbox