之前做了一个使用ListBox控件动态显示人员信息并且双击修改人员基本信息的功能,示意图如下:

  今天测试的时候发现只要有选中的对象,双击空白的地方也可以触发双击事件,查看代码才发现,是把事件写在ListBox上面了,所以只要双击ListBox就可以触发事件,修改代码,把事件放到DataTemPlate中:

   <DataTemplate x:Key="persontemplate">
                        <StackPanel x:Name="item" Orientation="Vertical">
                  <i:Interaction.Triggers>
                                <i:EventTrigger EventName="PreviewMouseDoubleClick" >
                                    <i:InvokeCommandAction Command="{Binding Path=DataContext.EditCommand}" CommandParameter="{Binding Content, RelativeSource ={RelativeSource AncestorType={x:Type ListBoxItem}}}"/>
                                </i:EventTrigger>
                              </i:Interaction.Triggers>
                              <Image  Width="145" Height="145" Stretch="Fill" Source="{Binding Path=show_pic,Converter={StaticResource FacePictureConverter},ConverterParameter=145|145,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> 
                            </Image>
                            <Grid Width="145" Height="22">
                                <TextBlock Text="{Binding p_name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" FontSize="14" Foreground="{StaticResource RichImageTextForeground}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
                            </Grid>
                        </StackPanel>
                    </DataTemplate>

运行,双击图片不能出触发,是因为StacKPanel 不支持PreviewMouseDoubleClick事件,于是在stackpanel外面套上ContentControl,但是运行的时候还是不能触发。

查找资料,给ListBox一个Name,将Command绑定时添加ElementName=personListBox,运行成功,代码如下:

            <ListBox x:Name="personListBox"  ItemsSource="{Binding PersonList}"  SelectedItem="{Binding CurrentPerson,Mode=TwoWay}"  ItemTemplate="{DynamicResource persontemplate}"   ItemContainerStyle="{StaticResource PersonListBoxStyle}">
                <ListBox.Template>
                    <ControlTemplate TargetType="{x:Type ListBox}">
                        <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" >
                            <WrapPanel Orientation="Horizontal" IsItemsHost="True" ScrollViewer.CanContentScroll="True"/>
                        </ScrollViewer>
                    </ControlTemplate>
                </ListBox.Template>
                <ListBox.Resources>
                    <DataTemplate x:Key="persontemplate">
                        <ContentControl>
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="PreviewMouseDoubleClick" >
                                    <i:InvokeCommandAction Command="{Binding ElementName=personListBox, Path=DataContext.EditCommand}" CommandParameter="{Binding Content, RelativeSource ={RelativeSource AncestorType={x:Type ListBoxItem}}}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                            
                            <StackPanel x:Name="item" Orientation="Vertical">
                                <Image  Width="145" Height="145" Stretch="Fill" Source="{Binding Path=show_pic,Converter={StaticResource FacePictureConverter},ConverterParameter=145|145,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> 
                               
                            </Image>
                            <Grid Width="145" Height="22">
                                <TextBlock Text="{Binding p_name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" FontSize="14" Foreground="{StaticResource RichImageTextForeground}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
                            </Grid>
                            
                        </StackPanel>
                        </ContentControl>
                    </DataTemplate>
                </ListBox.Resources>
               
            </ListBox>

虽然功能完成,但是不明白为什么,希望有人可以解惑。

 

posted on 2015-06-24 16:58  蓁蓁其叶  阅读(2531)  评论(0编辑  收藏  举报