2009-07-01 20 views
17

ओह, मैन। मुझे इस हफ्ते डब्ल्यूपीएफ दर्द महसूस हो रहा है। तो मेरी अगली चाल के लिए, अब मैं एक सूची दृश्य में प्रोग्रामेटिक रूप से किसी आइटम का चयन करने का तरीका समझने में असमर्थ हूं।WPF ListView प्रोग्रामेटिक रूप से आइटम का चयन करें

मैं सूचीदृश्य के आइटमकंटनर जेनरेटर का उपयोग करने का प्रयास कर रहा हूं, लेकिन यह काम नहीं कर रहा है। उदाहरण के लिए, 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 कुछ templating और खींचें/ड्रॉप कार्यक्षमता का उपयोग करता है, इसलिए मैं यहां 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

IsSelectedListViewItem की संपत्ति को अपने मॉडल पर एक संपत्ति में बांधें। फिर, आपको केवल यूआई की जटिलताओं के बारे में चिंता करने के बजाय अपने मॉडल के साथ काम करने की आवश्यकता है, जिसमें कंटेनर वर्चुअलाइजेशन के आसपास संभावित खतरे शामिल हैं।

उदाहरण के लिए:

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

अब, बस अपने मॉडल के IsGroovy संपत्ति के साथ चयन/ListView में आइटम का चयन रद्द करने के लिए काम।

+2

लेकिन निश्चित रूप से। अनिवार्य तथ्य। डब्लूपीएफ में किसी भी मुद्दे पर डाटाबेसिंग करने के लिए सभी समस्याओं को उबालें? धन्यवाद, केंट। –

+0

यह वास्तव में एक साफ समाधान है, कुडोस! –

+4

बस उल्लेख करें: यह समाधान विंडोज स्टोर ऐप पर काम नहीं करता है। इसके बजाय आपको पीछे कोड में बाध्यकारी सेट करना चाहिए, जैसे: http: //social.msdn.microsoft.com/Forums/windowsapps/en-US/9a0adf35-fdad-4419-9a34-a9dac052a2e3/listviewitemisselected-data-binding-in शैली-सेटर-है-नहीं काम? मंच = winappswithcsharp – SimonFisher

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); 
} 

क्या मैं देखने के अभ्यस्त कर रहा हूँ एक settable SelectedItem है, लेकिन मैं देख रहा हूँ आप सेट या यह करने के लिए नहीं जोड़ सकते हैं, लेकिन उम्मीद है कि यह विधि एक प्रतिस्थापन के रूप में काम करती है।

3

कहां 'यह' सूची दृश्य उदाहरण है। यह न केवल चयन को बदल देगा, बल्कि नए चुने हुए आइटम पर भी ध्यान केंद्रित करेगा।

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(); 
    } 
    } 
संबंधित मुद्दे