WPF Button doesn't execute Command

允我心安 提交于 2020-01-06 08:22:05

问题


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>

回答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.



来源:https://stackoverflow.com/questions/24953641/wpf-button-doesnt-execute-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!