2
<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>

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:

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

Can someone please help me?

niao
  • 4,972
  • 19
  • 66
  • 114

2 Answers2

6

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}"/>
Quartermeister
  • 57,579
  • 7
  • 124
  • 111
1

I don't understand statement 'because I am using style I cannot use SelectionChanged Event on ListView'

But you can use SelectionChanged event of Listview, if you are using Style also.

Ragunathan
  • 2,007
  • 1
  • 14
  • 10
  • 1
    actually I can't. There is a following error when I try to use it:Error 5 The event 'SelectionChanged' cannot be specified on a Target tag in a Style. Use an EventSetter instead. – niao Jun 29 '10 at 10:47
  • Try this – Ragunathan Jun 30 '10 at 05:00