WPF按钮不执行命令 [英] WPF Button doesn't execute Command

查看:82
本文介绍了WPF按钮不执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListViewItem ,其中有一个 Eventhandler ,并且在 ListViewItem ControlTemplate 内是一个 Button 的功能有所不同.但是,如果单击 Button ,则行为就像单击 ListViewItem 一样.

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 (我的ResourceDictionary):

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

当我在Appointmentoverview.xaml的 Window.Resource s中具有完整的 ListViewItem Style 时,此方法起作用.但是我不喜欢这样,因为那样会超出Styles.xaml的目的.另外,我没有 Button Style ,只是在 Button 内的所有 Styling 都具有.但这非常基础,现在我需要更复杂的 Styling ,所以我想创建一个单独的 Style .

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


更新:
如果我观察到我的EventHandler,则在按下Button时也会触发该事件.它只是说源是一个Button,但是命令没有执行.


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>

推荐答案

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

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.

另一方面, DataTemplate s 描述数据对象的视觉结构,因此您应该声明 DataTemplate s来定义数据的外观而不是使用 ControlTemplate s.因此,建议您阅读数据绑定概述页面上的MSDN,可以使您在继续使用WPF之前 有了更好的了解.

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.

更新项目后,您的问题可能会自动解决,但如果没有解决,请返回此处以使用新的XAML编辑问题.

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.

这篇关于WPF按钮不执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆