2009-12-10 9 views
5

ListBox의 더블 클릭 기능을 쉽게 빌드 할 수 있는지 알고 싶습니다. 저는 ListBoxItemSource이라는 콜렉션이 있습니다. 컬렉션에는 자체 데이터 유형이 포함됩니다. ListBox에서 DataTemplate을 사용하는 항목에 DoubleClick 사용

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

나는 StackPanel들과 TextBlock의 구성 내 Items, 대한 DataTemplate을 정의했다.

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

이제 두 번 클릭 된 목록 항목에 대한 두 번 클릭 이벤트를 감지하려고합니다. 현재 나는 다음과 같이 시도했지만, ListBox에 바인딩 된 항목을 반환하지 않기 때문에 작동하지 않지만 TextBlock입니다. 아이콘이없는 ListBoxItems을 경우

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

ListBoxitem에 더블 클릭 이벤트를 처리하는 청소 방법은 무엇입니까하지만 DataTemplates 자신의?

답변

12

당신이 당신의 ListBoxItem의 에 스타일을 적용 할 수이 DataTemplate을 적용하는 것이,

좋은 소식은 ... 나는이와 놀아 봤는데 내가 거기있어 생각 - 사람을

,369 같은

<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... 
} 
관련 문제