WPF DataGridRow在触发器上更改子控件的属性

时间:2015-12-11 08:37:59

标签: c# wpf grid

我有一个像这样的网格:

  <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20px" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="200px" />
        </Grid.ColumnDefinitions>
        <CheckBox Grid.Column="0" IsChecked="{Binding IsChecked, Mode = TwoWay}" VerticalAlignment="Center">
        </CheckBox>
        <StackPanel Grid.Column="1" Orientation="Vertical" >
            <Label Content="{Binding property1}" ></Label>
            <Label Content="{Binding property2}" ></Label>
        </StackPanel>
        <Button Grid.Column="2" Content="Click it" x:Name="myCoolbutton" Command="{Binding Parameter}"/>
    </Grid>

如果网格行上有MouseOver事件,我的计划是更改按钮的Visibility属性。实际上我用风格触发器尝试了这个,但是我无法达到我的目标,如果有人能给我一个提示,那将是令人愉快的。

谢谢

修改 以下是我之前工作的一些例子 - 它是评论,因为它无法正常工作

 <Grid.Style>
   <Style TargetType="{x:Type Grid}">
     <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
      <!-- <EventSetter Event="MouseMove" Handler="{Binding Rowhover}"></EventSetter>-->
      <!-- <Setter Property="Opacity" Value="0.5"></Setter>-->
      </Trigger>
      <!--<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Control,AncestorLevel=1}, Path=IsMouseOver}" Value="True" >
        <Setter Property="Visibility" Value="Hidden" />
      </DataTrigger>-->
      <!--<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Control,AncestorLevel=1}, Path=IsMouseOver}" Value="True">
         <Setter TargetName="copyToClipboardButton" Property="Visibility" Value="Hidden" />
      </DataTrigger>-->
    </Style.Triggers>
  </Style>

1 个答案:

答案 0 :(得分:0)

将此样式添加到Button中,当鼠标悬停在网格上时,将执行EnterActions属性,否则将执行ExitActions;通过将其Data与事件绑定到其祖先之一来有效地更改子控件属性。

<Style>
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=(IsMouseOver)}" Value="True">
           <Setter Property="SomeChildControlProperty" Value="SomeValue" />
        </DataTrigger>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=(IsMouseOver)}" Value="False">
           <Setter Property="SomeChildControlProperty" Value="SomeValue" />
        </DataTrigger>
    </Style.Triggers>
</Style>