WPF按钮不执行命令

时间:2014-07-25 10:29:19

标签: wpf xaml button event-handling listviewitem

我的ListViewItem有一个EventhandlerControlTemplate ListViewItem内的ButtonButton。但是,如果我点击ListViewItem,行为就像我点击<Window.Resources> <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource NormalListViewItem}"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" /> </Style> </Window.Resources> 一样。

AppointmentOverview.xaml:

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

Styles.xaml (我的资源字典)

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);
    }
}

AppointmentOverview.xaml.cs:

ListViewItem

当我在Appointmentoverview.xaml的Style中拥有完整的Window.Resource Style时,它就有效了。但我并不喜欢这样,因为它会超越Styles.xaml的目的。另外,我Button没有Styling,只是Button内的所有Styling。但这非常基础,现在我需要更复杂的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}"/>

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);
        }

    }

更新
如果我观察我的EventHandler,如果按下按钮也会触发。它只是说源是一个Button但命令没有执行。

Styles.xaml.cs

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

Styles.xaml

{{1}}

1 个答案:

答案 0 :(得分:1)

很明显,您的XAML不正确。 ControlTemplate主要用于指定可以在控件的多个实例之间共享的Control的视觉结构和行为方面。用简单的英语,这意味着它们用于更改 Control 的默认外观。如果这不是您正在做的事情,那么您不应该使用它们。

另一方面,DataTemplate s 描述数据对象的可视化结构,因此您应该声明DataTemplate来定义数据的外观,而不是使用ControlTemplate s。因此,我建议您阅读MSDN上的Data Binding Overview页面,这将为您提供更好的理解,之前您继续使用WPF。

当您更新项目时,您的问题可能会自行解决,但如果没有,请返回此处使用新的XAML编辑您的问题。