2009-12-10 15 views
5

ListBox için çift tıklama işlevselliğinin kolayca oluşturulabileceğini bilmek istiyorum.olarak bir koleksiyon ile bir ListBox var. Koleksiyon kendi veri tiplerini içerir. ListBox with DoubleClick on DataTemplate kullanarak öğeler

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

Ben StackPanel s ve TextBlock s oluşmaktadır benim Items, bir DataTemplate tanımladı.

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

Şimdi çift tıklanan liste öğesi için çift tıklama etkinliğini tespit etmek istiyorum. Şu anda aşağıdakilerle denedim, ancak işe yaramıyor çünkü Öğeye bağlı ListBox, ancak TextBlock. simgeler değil ListBoxItems ise

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

bir ListBox bir item üzerine çift tıklama olayı işlemek için temiz bir yolu, nedir, ancak DataTemplates kendi?

cevap

12

Eğer ListBoxItem bir stil uygulayabilir ve bir DataTemplate geçerli olduğunu,

iyi haber ... Bu etrafta oynuyorsun ve ben orada var düşünmek - birini

gibi

<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> 

ve sonra Pencerede bir işleyici uygulamak,: başka bir deyişle, diğer ...

engellemez, aşağıdaki gibi bir şey olabilir

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... 
} 
İlgili konular