2

在 WPF 中,我有一个绑定到 ObservableCollection 的列表视图。

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 中的文本块(可以吗?),我尝试通过MouseLeftButtonDown-Event. 但该事件从未收到!

C#(在后面的代码中):

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

我究竟做错了什么?如何接收活动?顺便说一句:上下文菜单的命令工作得很好:)

更新我发现了我的错误——>我在错误的用户控件中添加了事件。该死的,我的注意力不集中。很抱歉打扰大家。

4

2 回答 2

3

您可以简单地使用 blend sdk (System.Windows.Interactivity.dll) 中的 InvokeCommandAction

    <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:不知道你的类型这就是为什么反对

于 2012-07-05T12:37:24.687 回答
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!");
}
于 2012-07-05T12:31:33.800 回答