问题描述
我有一个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
中拥有完整的ListViewItem
Style
时,它起作用了.但是我不喜欢这样,因为那样会超出Styles.xaml的目的.另外,我没有Button
的Style
,只是Button
内部的所有Styling
.但这非常基础,现在我需要更复杂的Styling
,所以我想创建一个单独的Style
.
It worked when I had the complete ListViewItem
Style
in the Window.Resource
s 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
来定义数据的外观,而不是使用ControlTemplate
s.因此,建议您阅读数据绑定概述页面上的MSDN,可以使您在继续使用WPF之前 有了更好的了解.
On the other hand, DataTemplate
s Describe the visual structure of a data object, so you should be declaring DataTemplate
s to define what your data looks like instead of using ControlTemplate
s. 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按钮不执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!