WPF的ListView的SelectionChanged内部风格不起作用。 EventSetter要么 [英] WPF ListView SelectionChanged inside style does not work. EventSetter either

查看:251
本文介绍了WPF的ListView的SelectionChanged内部风格不起作用。 EventSetter要么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Style x:Key="OrderGroupTemplateStyle" TargetType="{x:Type ContentControl}">
<Style.Triggers>
   <DataTrigger Binding="{Binding Path=Name.ShowDetailedInfo, UpdateSourceTrigger=PropertyChanged}" Value="False">
      <Setter Property="ContentTemplate">
         <Setter.Value>
            <DataTemplate>
               <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="3" Margin="2">
                  <StackPanel Background="LightGoldenrodYellow">
                     <ContentControl Content="{Binding Path=.}" Style="{StaticResource MyRecordViewModelShortStyle}"/>
                    <ListView ItemsSource="{Binding Path=Items}" Margin="4">                                                                     
                    <ListView.ItemContainerStyle>
                       <Style TargetType="{x:Type ListViewItem}">
                          <Setter Property="HorizontalContentAlignment" Value="Stretch" />                            <Setter Property="Padding" Value="2"/>
                          <EventSetter Event="MouseDoubleClick" Handler="ItemsControl_SelectionChanged"/>
                                                        </Style>
                                                    </ListView.ItemContainerStyle>

我想,当列表视图选择改变做一些工作。因为我用的风格,我不能在ListView中使用SelectionChanged事件。我试图用EventSetter但是在编译项目中的任何错误:

I would like to do some job when listview selection changed. because I am using style I cannot use SelectionChanged Event on ListView. I tried to use EventSetter but there is any error while compiling the project:

事件MouseDoubleClick不能
  在样式一个目标标签中指定。
  使用EventSetter来代替。

The event 'MouseDoubleClick' cannot be specified on a Target tag in a Style. Use an EventSetter instead.

有人能帮帮我吗?

推荐答案

尝试创建样式作为一种资源,而不是在线声明它。我不知道为什么它的行为方式不同,但它似乎使错误消失:

Try creating the Style as a resource instead of declaring it inline. I don't know why it behaves differently, but it appears to make the error go away:

<Style TargetType="{x:Type ListViewItem}" x:Key="ItemContainerStyle">
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="Padding" Value="2"/>
    <EventSetter Event="MouseDoubleClick" Handler="ItemsControl_SelectionChanged"/>
</Style>
<Style x:Key="OrderGroupTemplateStyle" TargetType="{x:Type ContentControl}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Name.ShowDetailedInfo, UpdateSourceTrigger=PropertyChanged}" Value="False">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="3" Margin="2">
                            <StackPanel Background="LightGoldenrodYellow">
                                <ContentControl Content="{Binding Path=.}" Style="{StaticResource MyRecordViewModelShortStyle}"/>
                                <ListView ItemsSource="{Binding Path=Items}" Margin="4" ItemContainerStyle="{StaticResource ItemContainerStyle}"/>

这篇关于WPF的ListView的SelectionChanged内部风格不起作用。 EventSetter要么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆