假设我有一个简单的ListView,其中只有一个按钮,它是Gridview的唯一一栏:
<ListView x:Name="SomeListView" ItemsSource="{Binding Whatever}">
<ListView.View>
<GridView>
<GridViewColumn CellTemplate="{StaticResource myCellTemplate}" Header="ColumnHeader"/>
</GridView>
</ListView.View>
</ListView>
myCellTemplate看起来像这样:
<DataTemplate x:Key="myCellTemplate">
<Viewbox>
<Button x:Name="mybutton" Command="{Binding myClickCommand}" Content="{Binding btnBinding}"></Button>
</Viewbox>
</DataTemplate>
myClickCommand在我的VM中使用ICommand。此时,一切都很糟糕,用户点击我的按钮会调用myClickCommand,如你所料。现在,我想在我的行中添加一些鼠标悬停样式,所以我添加了一个ItemContainerStyle,如下所示:
<ListView x:Name="SomeListView" ItemContainerStyle="{StaticResource myItemStyle}" ItemsSource="{Binding Whatever}">
...
</ListView>
myItemStyle为:
<Style x:Key="myItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Grid>
<Border x:Name="borderMain" BorderThickness="1" BorderBrush="Transparent">
<GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<Rectangle x:Name="rectGlass" Fill="LightGray" Opacity=".2" Visibility="Hidden"></Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="borderMain" Property="BorderBrush" Value="DarkGray" />
<Setter TargetName="rectGlass" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
此时,用户的点击不会触发myClickCommand,可能是因为事件没有路由到按钮。有没有办法实现这个目标?
答案 0 :(得分:0)
您的Rectangle(rectGlass)位于Button的顶部。该按钮未收到点击事件,因为它实际上从未被点击过。