Listview控件模板。如何向命令添加事件?

时间:2017-04-07 08:28:55

标签: wpf listview

<ListView 
                    x:Name="ListViewPoducts"
                    SelectionMode="Single"
                    ItemsSource="{Binding ProductGroups}"
                    SelectedItem="{Binding SelectedProductGroup}"
                    ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                    ScrollViewer.VerticalScrollBarVisibility="Auto"
                    Background="{StaticResource NouvemLightBackgroundBrush}">

            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Vertical" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="Width" Value="Auto"/>
                    <Setter Property="Height" Value="Auto"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListViewItem">
                                <Border  Background="Transparent" >
                                    <Button Name="button" Style="{StaticResource StyleButtonTouchscreenMenu}" IsHitTestVisible="False">
                                        <Grid>
                                            <TextBlock Text="{Binding Name}" 
                                       FontSize="{Binding Source={x:Static global:ApplicationSettings.TouchscreenMenuButtonFontSize}}"
                                       HorizontalAlignment="Center" 
                                       VerticalAlignment="Center" 
                                       TextAlignment="Center" 
                                       TextWrapping="Wrap" />
                                        </Grid>
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="PreviewMouseUp">
                                                <command:EventToCommand Command="{Binding GroupSelectedCommand}" />
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                                    </Button>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="button"  Property="Background" Value="{StaticResource NouvemSecondaryMainBrush}"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.Resources>
                <Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource StyleScrollBarTouchscreenNarrow}"/>
            </ListView.Resources>
        </ListView>

您好,

我试图为listview控件模板中包含的按钮添加一个事件来命令触发器。我们有一个在客户端触摸屏上运行的应用程序,它对按钮按下过于敏感。我想处理一个按钮PreviewMouseUp事件来解决这个问题(目前当他们按下按钮时,它也会按下按钮下面的按钮,例如按下按钮两次)

但是,命令处理程序代码在我的视图模型中没有被命中。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

IsHitTestVisible设置为&#34; True&#34;按钮。

答案 1 :(得分:0)

<ListView 
                    x:Name="ListViewPoducts"
                    SelectionMode="Single"
                    ItemsSource="{Binding ProductGroups}"
                    SelectedItem="{Binding SelectedProductGroup}"
                    ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                    ScrollViewer.VerticalScrollBarVisibility="Auto"
                    Background="{StaticResource NouvemLightBackgroundBrush}">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Vertical" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="Width" Value="Auto"/>
                    <Setter Property="Height" Value="Auto"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListViewItem">
                                <Border  Background="Transparent" >

                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="PreviewMouseUp">
                                            <command:EventToCommand Command="{Binding DataContext.HandeProductionProductGroupSelectionCommand, ElementName=ListViewPoducts}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                    <Button Name="button" Style="{StaticResource StyleButtonTouchscreenMenu}" IsHitTestVisible="False">
                                        <Grid>
                                            <TextBlock Text="{Binding Name}" 
                                       FontSize="{Binding Source={x:Static global:ApplicationSettings.TouchscreenMenuButtonFontSize}}"
                                       HorizontalAlignment="Center" 
                                       VerticalAlignment="Center" 
                                       TextAlignment="Center" 
                                       TextWrapping="Wrap" />
                                        </Grid>
                                    </Button>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="button"  Property="Background" Value="{StaticResource NouvemSecondaryMainBrush}"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.Resources>
                <Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource StyleScrollBarTouchscreenNarrow}"/>
            </ListView.Resources>
        </ListView>

我明白了。只需将我的事件绑定到命令根元素数据上下文。

由于