c# - 如何在 'InputBindings'元素中正确执行 'Border'?

标签 c# wpf mvvm

我有一个WPF ListBox,它使用DataTemplate作为ItemTemplate。此DataTemplateBorder和几个子元素组成。

我希望有一个通用命令('SelectImageCommand')处理Border区域中的任何位置(任何子元素或空白边框本身)时单击鼠标左键。

我在InputBinding中放置了一个Border。该命令将按喜好执行,但不再发生“可视选择”(永久更改背景颜色)。如果我删除MouseBinding,则视觉选择效果很好。

通过为MouseLeftButtonDown事件添加事件处理程序,我能够实现非MVVM解决方法。

在我看来,MouseBinding以与处理事件处理程序并将HandledMouseButtonEventArgs属性设置为true相同的方式“处理”鼠标单击事件。

我该如何处理未捕获click事件的MVVM MouseBinding

我不是WPF专业人士。请尝试提供符合MVVM的解决方案。

<DataTemplate x:Key="ImageItemTemplate">
    <Border Padding="10" Width="325" Height="350"
            BorderBrush="{DynamicResource SignificantInformationColorBrush}"
            BorderThickness="0" Margin="5" Background="Transparent" Focusable="True" MouseLeftButtonDown="ImageBorderOnMouseLeftButtonDown">
        <!--<Border.InputBindings>
            <MouseBinding MouseAction="LeftClick"
                          Command="{Binding Path=DataContext.SelectImageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                          CommandParameter="{Binding}" />
        </Border.InputBindings>-->
        <StackPanel>
            <Border BorderBrush="LightGray"
                    BorderThickness="1"
                    CornerRadius="1" Height="300" Width="300" Background="Transparent">
                <StackPanel Background="Black">
                    <Canvas Width="300" Height="300" VerticalAlignment="Center"
                            HorizontalAlignment="Center" Background="Transparent">
                        <Image Width="300" Stretch="UniformToFill" StretchDirection="DownOnly"
                               Source="{Binding BitmapImage}"
                               Height="300">
                            <Image.InputBindings>
                                <MouseBinding MouseAction="LeftDoubleClick"
                                              Command="{Binding Path=DataContext.OpenOriginalCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                                              CommandParameter="{Binding}" />
                            </Image.InputBindings>
                        </Image>
                    </Canvas>
                </StackPanel>
            </Border>
            <StackPanel Orientation="Horizontal" Margin="1 10 0 0">
                <TextBlock Margin="5 0 0 0" TextAlignment="Center"
                           FontSize="11" Text="{Binding ImageInfo.SopInstanceUid}" Background="Transparent" />
            </StackPanel>
        </StackPanel>
    </Border>
</DataTemplate>

最佳答案

设置BackgroundBorder属性:

<Border Padding="10" Width="325" Height="350" BorderThickness="0" Margin="5"
        Background="Transparent">
...

如果不这样做,未绘制(paint)的区域将不会响应鼠标事件。

关于c# - 如何在 'InputBindings'元素中正确执行 'Border'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54632693/

相关文章:

c# - Web API 返回 XML 内容

c# - 使用 MVVM 的动态 View 动画

c# - 如何创建具有只能具有特定值的属性的对象

c# - 按 Enter 时停止 'Ding'

c# - EmguCV 3.0 中的 CvInvoke.ConvexityDefects()

wpf - 在 Windows XP 中运行 .NET 4.0 WPF 应用程序时与 ICommand 相关的错误

c# - 响应式扩展 - 返回相同的可观察对象还是创建新的?

c# - 无法使用 DataTemplate WPF 将 InputBinding 应用于整个 ListBoxItem

c# - Fluent NHibernate 忽略 ClassMap 中的属性,使用 FluentMappings

.net - INotifyPropertyChanged 属性名称 - 硬编码与反射?