【问题标题】:DoubleClick on my ListBox item is not being fired没有触发我的 ListBox 项上的 DoubleClick
【发布时间】:2019-08-06 14:39:20
【问题描述】:

我正在使用 MVVM 方法实现一个列表框,但我遇到了一个问题,即我的双击命令没有被触发。当我启动我的应用程序时,我看到我的 ListBox 通过绑定填充得很好。但是当我双击一个项目时,什么也没有发生。有什么我想念的吗?提前谢谢了。

这是我的 UserControl (xaml) 设置方式

    <ListBox 
        x:Name="Files" 
        ItemsSource="{Binding FileCollection, Mode=TwoWay}"
        SelectedItem="{Binding Filename, Mode=TwoWay}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}">                                    
                    <TextBlock.InputBindings>
                        <MouseBinding  
                         Gesture="LeftDoubleClick" 
                         Command="{Binding EditFileCommand}"/>
                    </TextBlock.InputBindings>
               </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

这就是我在 View Model 中设置命令对象的方式:

        //..using system.windows.input for ICommand
        private ICommand editFileCommand = null;
        public ICommand EditFileCommand
        {
            get
            {
                //RelayCommand comes from GalaSoft.MvvmLight.Command
                return editFileCommand ?? new RelayCommand(EditFile); 
            }
        }

        private void EditFile()
        {
            MessageBox.Show("Double Click!");
        }

【问题讨论】:

  • 您是否已经签出those SO questions
  • @dymanoid 感谢您的及时回复。我检查了它们,但它们似乎没有使用 MVVM。
  • @user2529011:命令在哪里定义?

标签: c# wpf data-binding


【解决方案1】:

这和RelayCommand差不多,你可以这样使用:

在您的 ViewModel 中声明:

public RelayCommand EditFileCommand { get; set; }

然后,你需要初始化它:

EditFileCommand = new RelayCommand(EditFile);

XAML 保持不变:

<ListBox 
        x:Name="Files" 
        ItemsSource="{Binding FileCollection, Mode=TwoWay}"
        SelectedItem="{Binding Filename, Mode=TwoWay}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}">                                    
                    <TextBlock.InputBindings>
                        <MouseBinding  
                         Gesture="LeftDoubleClick" 
                         Command="{Binding EditFileCommand}"/>
                    </TextBlock.InputBindings>
               </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

【讨论】:

  • thaaaaaaaaank youuuuuu!我对 RelayCommand 做了更多的挖掘,它不仅继承自 ICommand,而且只是创建了我的对象 RelayCommand,我不必对我的 EditFileCommand 做所有额外的事情。只需一行即可初始化并完成。再次感谢你。摇滚吧!
【解决方案2】:

如果命令属性是在File 类中定义的,那么只要您实际单击TextBlock,它就应该可以工作。您可以使用ItemContainerStyle 使TextBlock 跨越ListBoxItem 容器:

<ListBox x:Name="Files" ...>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            ...
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

如果在定义了FileCollection 属性的视图模型中定义了命令,则应使用RelativeSource

<TextBlock.InputBindings>
    <MouseBinding  Gesture="LeftDoubleClick" 
                   Command="{Binding DataContext.EditFileCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"/>
</TextBlock.InputBindings>

【讨论】:

    猜你喜欢
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2013-10-13
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多