2009-07-01 73 views
17

噢,伙计。我本周感受到WPF的痛苦。所以对于我的下一个技巧,我现在无法弄清楚如何在ListView中以编程方式选择一个项目。WPF ListView以编程方式选择项目

我试图使用listview的ItemContainerGenerator,但它似乎并没有工作。例如,obj是下面的操作后,空:

//VariableList is derived from BindingList 
m_VariableList = getVariableList(); 
lstVariable_Selected.ItemsSource = m_VariableList; 
var obj = 
    lstVariable_Selected.ItemContainerGenerator.ContainerFromItem(m_VariableList[0]); 

我试过(基于这里看到的建议等场所)使用ItemContainerGenerator的StatusChanged事件,但无济于事。事件从未发生。例如:

m_VariableList = getVariableList(); 
lstVariable_Selected.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged); 
lstVariable_Selected.ItemsSource = m_VariableList; 

... 

void ItemContainerGenerator_StatusChanged(object sender, EventArgs e) 
{ 
    //This code never gets called 
    var obj = lstVariable_Selected.ItemContainerGenerator.ContainerFromItem(m_VariableList[0]); 
} 

这整个事情的症结在于我只是想在我的ListView中预先选择一些项目。为了不留下任何东西,ListView使用了一些模板和拖放功能,所以我在这里包含了XAML。实质上,这个模板使每个项目都带有一些文本的文本框 - 当选择任何项目时,复选框被选中。而每个项目也得到它下面一点点字形插入新的项目(这一切工作正常):

<DataTemplate x:Key="ItemDataTemplate_Variable"> 
<StackPanel> 
    <CheckBox x:Name="checkbox" 
     Content="{Binding Path=ListBoxDisplayName}" 
     IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected}" /> 
    <Image ToolTip="Insert Custom Variable" Source="..\..\Resources\Arrow_Right.gif" 
     HorizontalAlignment="Left" 
     MouseLeftButtonDown="OnInsertCustomVariable" 
     Cursor="Hand" Margin="1, 0, 0, 2" Uid="{Binding Path=CmiOrder}" /> 
</StackPanel> 
</DataTemplate> 

... 

<ListView Name="lstVariable_All" MinWidth="300" Margin="5" 
    SelectionMode="Multiple" 
    ItemTemplate="{StaticResource ItemDataTemplate_Variable}" 
    SelectionChanged="lstVariable_All_SelectionChanged" 
    wpfui:DragDropHelper.IsDropTarget="True" 
    wpfui:DragDropHelper.IsDragSource="True" 
    wpfui:DragDropHelper.DragDropTemplate="{StaticResource ItemDataTemplate_Variable}" 
     wpfui:DragDropHelper.ItemDropped="OnItemDropped"/> 

所以我缺少什么?如何以编程方式选择ListView中的一个或多个项目?

非常感谢您的时间。

回答

30

ListViewItemIsSelected属性绑定到模型上的属性。然后,您只需要使用您的模型,而不必担心UI的复杂性,这包括容器虚拟化潜在的危险。

例如:

<ListView> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="ListViewItem"> 
      <Setter Property="IsSelected" Value="{Binding IsGroovy}"/> 
     </Style> 
    </ListView.ItemContainerStyle> 
</ListView> 

现在,只需用你的模型的IsGroovy物业工作选择/取消选择ListView项目。

+2

但是当然。数据绑定。 WPF中的所有问题都不是在某种程度上归结为数据绑定?谢谢,肯特。 – 2009-07-01 15:52:44

4

这里是我最好的猜测,这将是一个更简单的选择方法。因为我不知道你选择什么,这里有一个普通的例子:

var indices = new List<int>(); 

for(int i = 0; i < lstVariable_All.Items.Count; i++) 
{ 
    // If this item meets our selection criteria 
    if(lstVariable_All.Items[i].Text.Contains("foo")) 
    indices.Add(i); 
} 

// Reset the selection and add the new items. 
lstVariable_All.SelectedIndices.Clear(); 

foreach(int index in indices) 
{ 
    lstVariable_All.SelectedIndices.Add(index); 
} 

我用什么来看到的是可设置的SelectedItem,但我看到你不能设置或添加到此,但希望这种方法可以作为替代品。

3

其中'this'是ListView实例。这不仅会改变选择,还会将焦点放在新选择的项目上。

private void MoveSelection(int level) 
    { 
    var newIndex = this.SelectedIndex + level; 
    if (newIndex >= 0 && newIndex < this.Items.Count) 
    { 
     this.SelectedItem = this.Items[newIndex]; 
     this.UpdateLayout(); 
     ((ListViewItem)this.ItemContainerGenerator.ContainerFromIndex(newIndex)).Focus(); 
    } 
    } 
相关问题