1

I would like to add a Click Handler to my ListViewItems. I have found many Tutorials on how to do that but I have problems recreating that inside my ResourceDictionary. My Question is how can I define the ItemContainerStyle there. It doesn't work like this:

<Style TargetType="{x:Type ListViewItem}">        
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border BorderBrush="#5076A7" BorderThickness="1">
                    <Border.Background>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                            <GradientStop Color="#FFFFFF" Offset="0.0"/>
                            <GradientStop Color="#C0D3EA" Offset="1.0"/>
                        </LinearGradientBrush>
                    </Border.Background>
                    <StackPanel TextElement.FontFamily="Segoe UI" TextElement.FontSize="12">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="15"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TextBlock Padding="3,0,0,0" Text="{Binding Betreff}" TextTrimming="CharacterEllipsis" Grid.Column="0" Grid.Row="0"/>
                            <Button FontSize="7" Content="X" Grid.Column="1" Grid.Row="0" 
                                    Command="{Binding DataContext.DeleteButtonCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
                                    CommandParameter="{Binding ItemId}"/>
                        </Grid>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Padding="3,0,0,0" Text="{Binding Kunde}"/>
                            <TextBlock Padding="3,0,0,0" Text="|"/>
                            <TextBlock Padding="3,0,0,0" Text="{Binding IncidentId}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock FontWeight="Bold" Padding="3,0,0,0" Text="{Binding Ort}"/>
                            <TextBlock Padding="3,0,0,0" Text="("/>
                            <TextBlock Text="{Binding Alternative}"/>
                            <TextBlock Text=")"/>
                        </StackPanel>
                    </StackPanel>
                </Border>
            </ControlTemplate>                
        </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Betreff}" Value="Blocked">
            <Setter Property="Template" Value="{StaticResource BlockedListViewItem}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
BluePalmTree
  • 299
  • 3
  • 23
  • "It doesn't work like this" means what exactly? Please be more specific. – Clemens Jul 23 '14 at 09:26
  • I tried to recreate this http://stackoverflow.com/questions/10207888/wpf-listview-detect-when-selected-item-is-clicked but just that the style is in a ResourceDictionary. It says `The member "ItemContainerStyle" is not recognized or is not accessible.` – BluePalmTree Jul 23 '14 at 09:40
  • You didn't copy that XAML correctly. And it won't work in a separate ResourceDictionary file, as the event handler has to defined in some class (usually your MainWindow class). – Clemens Jul 23 '14 at 09:42

1 Answers1

0

Move the Style to your MainWindow's Resources and add the EventSetter directly to the ListViewItem Style:

<Style TargetType="ListViewItem">
    ...
    <EventSetter Event="PreviewMouseLeftButtonDown"
                 Handler="ListViewItem_PreviewMouseLeftButtonDown"/>
</Style>
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • If I do that I get the follwoing Error: `'ResourceDictionary' root element requires a x:Class attribute to support event handlers in the XAML file. Either remove the event handler for the PreviewMouseLeftButtonDown event, or add a x:Class attribute to the root element.` – BluePalmTree Jul 23 '14 at 09:55
  • 1
    As I've said already in a previous comment, it won't work in a separate ResourceDictionary, because you need a class that defines the event handler. And the error message is pretty clear about that. You may put the Style into your MainWindow's Resources instead. – Clemens Jul 23 '14 at 09:59
  • I have added the Style to the `MainWindow's Resources` and it works. But the Code behind is now in the `MainWindow.cs`. From what I know about `WPF` that's not very `MVVM`. – BluePalmTree Jul 23 '14 at 10:06
  • A `PreviewMouseLeftButtonDown` handler is part of the view. It neither belongs to the model nor the view model part of your application. – Clemens Jul 23 '14 at 10:12