2009-12-10 14 views
5

का उपयोग कर आइटम पर डबलक्लिक के साथ ListBox मैं जानना चाहता हूं कि ListBox के लिए डबल-क्लिकिंग कार्यक्षमता आसानी से बनाई जा सकती है या नहीं। मेरे पास है जो संग्रह के साथ ItemSource है। संग्रह में स्वयं के डेटा-प्रकार होते हैं।DataTemplate

<ListBox ItemsSource="{Binding Path=Templates}" 
     ItemTemplate="{StaticResource fileTemplate}"> 

मैं अपने Items, StackPanel और TextBlock रों के होते हैं जिसके लिए एक DataTemplate परिभाषित किया।

<DataTemplate x:Key="fileTemplate"> 
    <Border> 
     <StackPanel> 
       <TextBlock Text="{Binding Path=Filename}"/> 
       <TextBlock Text="{Binding Path=Description}"/> 
     </StackPanel> 
    </Border> 
</DataTemplate> 

अब मैं डबल-क्लिक सूची-आइटम के लिए डबल-क्लिक-ईवेंट का पता लगाना चाहता हूं। वर्तमान में मैंने निम्नलिखित के साथ प्रयास किया, लेकिन यह काम नहीं करता है क्योंकि यह आइटम को ListBox पर वापस नहीं करता है लेकिन TextBlock है।

if (TemplateList.SelectedIndex != -1 && e.OriginalSource is Template) 
{ 
    this.SelectedTemplate = e.OriginalSource as Template; 
    this.Close(); 
} 

एक ListBox में एक item पर एक डबल क्लिक करें घटना को संभालने के लिए एक साफ तरीका क्या है, अगर माउस नहीं ListBoxItems रहे हैं, लेकिन खुद DataTemplates? एक -

उत्तर

12

मैं चारों ओर इस के साथ खेल किया गया है और मुझे लगता है मैं वहाँ मिल गया है लगता है ...

अच्छी खबर है, कि आप अपने ListBoxItem पर शैली लागू कर सकते हैं और एक DataTemplate लागू

<Window.Resources> 
     <DataTemplate x:Key="fileTemplate" DataType="{x:Type local:FileTemplate}"> 
... 
     </DataTemplate> 
    </Window.Resources> 

    <Grid> 

     <ListBox ItemsSource="{Binding Templates}" 
       ItemTemplate="{StaticResource fileTemplate}"> 
      <ListBox.ItemContainerStyle> 
       <Style TargetType="{x:Type ListBoxItem}"> 
        <EventSetter Event="MouseDoubleClick" Handler="DoubleClickHandler" /> 
       </Style> 
      </ListBox.ItemContainerStyle> 
     </ListBox> 

    </Grid> 

और फिर अपने विंडो में कोई हैंडलर को लागू,

की तरह: अन्य ...

दूसरे शब्दों में बाधा नहीं है, तो आपको निम्न की तरह कुछ हो सकता है

public void DoubleClickHandler(object sender, MouseEventArgs e) 
{ 
    // item will be your dbl-clicked ListBoxItem 
    var item = sender as ListBoxItem; 

    // Handle the double-click - you can delegate this off to a 
    // Controller or ViewModel if you want to retain some separation 
    // of concerns... 
} 
संबंधित मुद्दे