2017-10-10 65 views
0

比方说,我有一个按钮,在它的GridView的只是列一个简单的ListView:处理按钮的点击命令里面的ListView

<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一个ICommand在我的VM中。在这一点上,一切都是疯狂的,用户点击我的按钮就会像您期望的那样调用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

您的矩形(rectGlass)位于按钮的顶部。该按钮没有收到点击事件,因为它实际上没有被点击。

+0

嗯。好吧,我看到我可以更改myItemStyle中控件的顺序,将边框置于rectGlass Rectangle的“顶部”。这可以让按钮被点击,但风格并不是我想要的。无论如何,这回答了这个问题。谢谢。 – sabelbe