0

I have a ListViewItem which has an Eventhandler attatched and inside the ControlTemplate of the ListViewItem is a Button which does something different. But if I click the Button, the Behaviour is like I clicked on the ListViewItem.

AppointmentOverview.xaml:

<Window.Resources>        
    <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource NormalListViewItem}">
        <EventSetter Event="PreviewMouseLeftButtonDown" 
            Handler="ListViewItem_PreviewMouseLeftButtonDown" />
    </Style>
</Window.Resources>

Styles.xaml(My ResourceDictionary):

<!--Style für die normalen ListViewItems-->
<Style x:Key="NormalListViewItem" 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="#FFFEB603" 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 Name="Betreff" Padding="3,0,0,0" Text="{Binding Betreff}" TextTrimming="CharacterEllipsis" Grid.Column="0" Grid.Row="0"/>
                            <Button Grid.Column="1" Grid.Row="0" Style="{StaticResource ListViewItemButton}"/>

AppointmentOverview.xaml.cs:

private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var item = sender as ListViewItem;
    if (item != null)
    {
        AppointmentOverviewViewModel apvm = this.DataContext as AppointmentOverviewViewModel;
        apvm.editAppointment(item);
    }
}

It worked when I had the complete ListViewItem Style in the Window.Resources of Appointmentoverview.xaml. But I didn't like that because the would kind of beat the purpose of Styles.xaml. Also I didn't have a Style for the Button, just did all the Styling inside the Button. But this was very Basic and now I need more complex Styling so I wanted to create a separate Style.

<Button FontSize="7" Content="X" Grid.Column="1" Grid.Row="0" 
    Command="{Binding DataContext.DeleteButtonCommand, RelativeSource={
    RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{Binding ItemId}"/>  

Update:
If I observe my EventHandler this is also triggered if the Button is pressed. It just says the source is a Button but the command is not executed.

Styles.xaml.cs

void ListViewItem_MouseLeftDown(object sender, MouseButtonEventArgs e)
    {
        DependencyObject current = sender as DependencyObject;
        while (current != null && current.GetType() != typeof(ListViewItem))
        {
            current = VisualTreeHelper.GetParent(current);
        }
        var item = current as ListViewItem;
        if (item != null)
        {
            Window parent = Window.GetWindow(current);
            AppointmentOverviewViewModel apovm = parent.DataContext as AppointmentOverviewViewModel;
            apovm.editAppointment(item);
        }

    }

Styles.xaml

<!--DataTemplate für die normalen ListViewItems-->
<DataTemplate DataType="{x:Type local:SCSMAppointment}">
    <Border BorderBrush="#5076A7" BorderThickness="1" PreviewMouseLeftButtonDown="ListViewItem_MouseLeftDown">
        <Border.Background>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                <GradientStop Color="#FFFFFF" Offset="0.0"/>
                <GradientStop Color="#FFFEB603" Offset="1.0"/>
            </LinearGradientBrush>
        </Border.Background>
        <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
            <DockPanel Grid.Row="0">
                <Button FontSize="7" Content="X" DockPanel.Dock="Right" Width="15"
                                        Command="{Binding DataContext.DeleteButtonCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
                                        CommandParameter="{Binding ItemId}"/>
                <TextBlock Name="Betreff" Padding="3,0,0,0" Text="{Binding Betreff}" TextTrimming="CharacterEllipsis" />

            </DockPanel>
            <StackPanel Orientation="Horizontal" Grid.Row="1">
                <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" Grid.Row="2">
                <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>
        </Grid>
    </Border>        
</DataTemplate>
BluePalmTree
  • 299
  • 3
  • 23
  • Why are you using a `ControlTemplate` for your `ListViewItem`? Surely, you should be declaring what your data objects look like in a `DataTemplate` set to the `ItemTemplate` property instead. – Sheridan Jul 25 '14 at 10:44
  • I'm not sure. I'm new to WPF/Templating and this has always worked for me so far. I was just wodnering why the `Button` stopped working. – BluePalmTree Jul 25 '14 at 10:48

1 Answers1

1

It is apparent that your XAML is not correct. A ControlTemplate is primarily used to Specify the visual structure and behavioral aspects of a Control that can be shared across multiple instances of the control. In plain English, that means that they are used to change the default look of a Control. If that is not what you are doing, then you should not be using them.

On the other hand, DataTemplates Describe the visual structure of a data object, so you should be declaring DataTemplates to define what your data looks like instead of using ControlTemplates. Therefore, I suggest that you read the Data Binding Overview page on MSDN, which will give you a much better understanding, before you continue to work with WPF.

When you have updated your project, your problem will probably fix itself, but if it doesn't please return here to edit your question with your new XAML.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • I have updated my Project. But now only the `ControlTemplate` is visible. So just the `Background` is changed but no `Data` is visible. – BluePalmTree Jul 25 '14 at 11:25
  • Once again, you shouldn't be using a `ControlTemplate`. I provided you with several links to help you, but it appears that you have completely ignored them, so I'm not sure it's worth responding to your comments any more. You *cannot* learn WPF on this website... that's your job. – Sheridan Jul 25 '14 at 11:42
  • I'm sorry I asked to quickly. But now I have put everything into a `Datatemplate` but still when I click the `Button` the behaviour is like clicking on the `Ite`m. If I disable the `EventHandler` it works. – BluePalmTree Jul 25 '14 at 11:50