WPF ListView SelectionChanged内部样式不起作用。 EventSetter也是

时间:2010-06-29 10:23:34

标签: wpf listview selectionchanged

<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选择改变时,我想做一些工作。因为我正在使用样式我不能在ListView上使用SelectionChanged事件。我尝试使用EventSetter,但在编译项目时出现任何错误:

  

事件'MouseDoubleClick'不能   在样式中的Target标记上指定。   改为使用EventSetter。

有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:6)

尝试将Style创建为资源,而不是将其声明为内联。我不知道它为什么表现不同,但似乎让错误消失了:

<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}"/>

答案 1 :(得分:1)

我不懂语句'因为我使用的风格我不能在ListView上使用SelectionChanged事件'

但是如果你也使用Style,你可以使用Listview的SelectionChanged事件。