2015-04-14 84 views
0

我有这样的代码,它的工作原理:如何在WPF网格设置样式

<Grid Height="Auto" Width="Auto" HorizontalAlignment="Center" 
    Margin="15,50,0,25" Grid.Row="8" MouseLeftButtonDown="buttonFermaNAO_Click"> 
<Image Margin="0,10,0,10" 
     Width="206" Height="46" 
     HorizontalAlignment="Center"> 
    <Image.Style> 
    <Style TargetType="{x:Type Image}"> 
     <Setter Property="Source" Value="./Resources/Tasto_esegui_senza scritta_ROLL_OFF.png"/> 
     <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Source" Value="./Resources/Tasto start_senza scritta_Roll_ON.png"/> 
     </Trigger> 
     </Style.Triggers> 
    </Style> 
    </Image.Style> 
</Image> 
<Label HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Roboto-Bold" 
    FontSize="25" Foreground="White" >Ferma NAO</Label> 
</Grid> 

有了这个代码,当我将鼠标移动到图像被改变的图像,这是确定,但在该中心图像我有标签,我将鼠标移动到标签上,图像没有被改变,也没有确定。

我虽然如果有可能删除图像代码,并在网格设置风格,这可能吗?

这是我现在有结果:

enter image description here

,如果我把鼠标移动到按钮上我有这样的:

enter image description here

回答

1

您可以使用您解决问题DataTrigger绑定到您的网格IsMouseOver属性:

<Grid Height="Auto" Width="Auto" HorizontalAlignment="Center" 
Margin="15,50,0,25" Grid.Row="8" x:Name="myGrid" MouseLeftButtonDown="buttonFermaNAO_Click"> 
    <Image Margin="0,10,0,10" 
    Width="206" Height="46" 
    HorizontalAlignment="Center"> 
     <Image.Style> 
      <Style TargetType="{x:Type Image}"> 
       <Setter Property="Source" Value="./Resources/Tasto_esegui_senza scritta_ROLL_OFF.png"/> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding IsMouseOver, ElementName=myGrid}" Value="True"> 
         <Setter Property="Source" Value="./Resources/Tasto start_senza scritta_Roll_ON.png"/> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </Image.Style> 
    </Image> 
    <Label HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Roboto-Bold" 
     FontSize="25" Foreground="White" >Ferma NAO</Label> 
</Grid> 
+0

您的解决方案:) – bircastri