【发布时间】:2014-09-04 17:58:41
【问题描述】:
每当将一个项目添加到 ObservableCollection 时,我都会尝试在 ListView 中自动选择一个项目。我正在使用 CollectionChanged 事件来监听添加项目的时间,然后选择它。 CollectionChanged 事件似乎发生在 UI 更新之前,因此 SelectedIndex 会相应调整。我尝试设置 SelectedIndex 和 SelectedItem ,但在这两种情况下,添加后的项目最终都被选中。正确的索引是当集合更改时,UI 更新,然后索引由 ListView 递增。
这种现象可以用以下方式来证明:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" DataContext="{Binding Main, Source= {StaticResource Locator}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<ListView ItemsSource="{Binding Items, Mode=TwoWay}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" Grid.Row="0">
</ListView>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<Button Content="Add Item" Width="75" Command="{Binding AddItemCommand}"/>
<Label Content="SelectedIndex:"/>
<Label Content="{Binding SelectedIndex}"/>
<Label Content="SelectedItem:"/>
<Label Content="{Binding SelectedItem}"/>
<Label Content="<- Numbers should match after item added"/>
</StackPanel>
</Grid>
</Window>
和 ViewModel:
public class MainViewModel : ViewModelBase
{
private ICommand addItemCommand;
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
Items = new ObservableCollection<string>();
Items.CollectionChanged += Items_CollectionChanged;
}
private int selectedIndex = -1;
public const string SelectedIndexPropertyName = "SelectedIndex";
public int SelectedIndex
{
get
{
return selectedIndex;
}
set
{
if (selectedIndex != value)
{
selectedIndex = value;
RaisePropertyChanged(SelectedIndexPropertyName);
}
}
}
private string selectedItem = null;
public const string SelectedItemPropertyName = "SelectedItem";
public string SelectedItem
{
get
{
return selectedItem;
}
set
{
if (selectedItem != value)
{
selectedItem = value;
RaisePropertyChanged(SelectedItemPropertyName);
}
}
}
private ObservableCollection<string> items;
public const string ItemsPropertyName = "Items";
public ObservableCollection<string> Items
{
get
{
return items;
}
set
{
items = value;
RaisePropertyChanged(ItemsPropertyName);
}
}
private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
//SelectedItem = e.NewItems[0];
SelectedIndex = e.NewStartingIndex;
}
}
public ICommand AddItemCommand
{
get
{
if (addItemCommand == null)
addItemCommand = new RelayCommand(() => Items.Add("Item " + items.Count));
return addItemCommand;
}
}
}
我已将示例解决方案上传到www.itzalive.co.uk/AddItemSelection.zip
有人知道如何使用 CollectionChanged 事件选择新添加的项目吗?在我的实际程序中,该项目没有添加到它正在显示的同一位置,因此无法单独设置所选项目。
【问题讨论】:
-
C# 中的 SelectedItem 属性在哪里?
-
没有包含它,因为它没有被使用它基本上看起来与 SelectedIndex 属性完全相同
-
SelectedItem和SelectedIndex绑定同一个东西,所以我不建议同时设置。如果您删除SelectedItem绑定,它会起作用吗? -
我已确保我只更改代码中的两个属性中的一个或另一个,因此它们之间没有干扰。
-
不,只绑定一个或另一个似乎没有什么区别。
标签: c# wpf listview mvvm observablecollection