2012-07-05 2 views
1

WPF에서 ObservableCollection에 바인딩 된 listview가 있습니다.listview의 datatemplate에서 이벤트를 구독하는 방법

XAML : 나는 실제로 수신을 원하는 무엇

<ListView Name="listView" DockPanel.Dock="Top" ItemsSource="{Binding Path=ListOfOldData}" SelectedItem="{Binding Path=SelectedOldData, Mode=TwoWay}" SelectionMode="Single"> 
     <ListView.ContextMenu> 
      <ContextMenu> 
       <Button Content="Load" Command="{Binding Path=LoadCommand}" Name="loadButton" Height="23" Width="75" DockPanel.Dock="Left"/>    
       <!-- Is working just fine -->   
      </ContextMenu> 
     </ListView.ContextMenu> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <TextBlock MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" 
          Text="{Binding Path=Name}" FontWeight="Bold"><TextBlock Text=" - " FontWeight="Normal"/><TextBlock Text="{Binding Path=UpdateDatum}" FontWeight="Normal"/></TextBlock> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

인을 두 번 클릭하여 선택 항목에. xaml의 텍스트 블록에 명령을 바인딩 할 수 없기 때문에 (0125) 그러나이 행사는 결코 받아 들여지지 않습니다! (코드에서 뒤에)

C 번호 :

private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    Console.WriteLine("MouseLeftButtonDown received!"); 
} 

내가 잘못 뭐하는 거지? 어떻게 행사를받을 수 있습니까? Btw : 컨텍스트 메뉴의 명령이 잘 작동하고 있습니다.

업데이트 오류가 발생했습니다.> 잘못된 usercontrol에서 이벤트를 추가했습니다. 젠장. 너를 도청해서 미안해.

답변

1

ListView에는 DoubleClick MouseEvent가 있습니다. 이 그것을 수행해야합니다

<ListView MouseDoubleClick="DoubleClickOnIt"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
     <TextBlock Text="{Binding Path=Name}" FontWeight="Bold"> 
      <TextBlock Text=" - " FontWeight="Normal"/> 
      <TextBlock Text="{Binding Path=UpdateDatum}" FontWeight="Normal"/> 
     </TextBlock> 
     </DataTemplate> 
    </ListView.ItemTemplate> 

    <ListViewItem> 
     dddd 
    </ListViewItem> 
    <ListViewItem> 
     eeeee 
    </ListViewItem> 
</ListView> 

그리고 뒤에있는 코드 :

private void DoubleClickOnIt(object sender, MouseButtonEventArgs e) 
{ 
    var listView = sender as ListView; 
    var selectedItem = listView.SelectedItem; 
    Console.WriteLine("received!"); 
} 
+0

저에게는 효과가 없습니다. 이유는 모르지만 이벤트 처리기에서는 실행되지 않습니다. – basti

+1

내가 가지고있는 것이 무엇인지 모르겠다. 컴파일하고 여기에서 작업하고있다. :) – MBen

+0

내 listview가 확장기에 있기 때문에 문제가 될 가능성이 있습니까? – basti

3
단순히 혼합 SDK에서 InvokeCommandAction을 사용할 수 있습니다

(System.Windows.Interactivity.dll)

<ListView x:Name="lvw" ItemsSource="{Binding ListOfOldData}"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="MouseDoubleClick"> 
       <i:InvokeCommandAction Command="{Binding Path=OpenCommand}" 
              CommandParameter="{Binding ElementName=lvw, Path=SelectedItem}"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 

    </ListView> 

편집을 : viewmodel은 다음과 같이 표시되어야합니다.

public List<object> ListOfOldData{ get; set; } 

    private DelegateCommand<object> _openCommand;//or RelayCommand 

    public DelegateCommand<object> OpenCommand 
    { 
     get { return _openCommand?? (this._openCommand= new DelegateCommand<object>(this.Execute)); } 
    } 

    private void Execute(object obj) 
    { 
     //obj is your selectedItem 
    } 

ps : dunno yours thats why 객체

+0

MVVM Light의 EventToCommand도 같은 방식으로 작동합니다. – blindmeis

+0

하지만 저를 위해 일하지 않습니다 :( 명령이 호출되지 않았습니다. 가능한 문제는 무엇입니까? 어떤 아이디어가 있습니까? – basti

+1

대 출력 창에 바인딩 오류가 있습니까? 그렇지 않으면 viewmodel에서 모든 관련 코드를 게시하고 확인해야합니다. – blindmeis

관련 문제