简体   繁体   English

隐藏复选框直到鼠标悬停在 WPF 上

[英]Hide checkbox until mouse over WPF

I am new to XAML, but I would like the CheckBox option to be hidden on my application until the user mouses over the row and can check the box from there.我是 XAML 的新手,但我希望CheckBox选项隐藏在我的应用程序中,直到用户将鼠标悬停在该行上并可以从那里选中该框。 Here is what I currently have and I'm not sure why it doesn't work.这是我目前拥有的,我不确定为什么它不起作用。

<DataGridTemplateColumn.CellTemplate>
   <DataTemplate>
      <CheckBox Name="cbkSelect" 
                IsChecked="{Binding Path=IsSelectedForOrder, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
         <CheckBox.Style>
            <Style TargetType="CheckBox">
               <Setter Property="Visibility" Value="Hidden"/>
               <Style.Triggers>
                  <DataTrigger Binding="{Binding ElementName=cbkSelect, Path=IsMouseOver}" Value="True">
                     <Setter Property="Visibility" Value="Visible"/>
                  </DataTrigger>
               </Style.Triggers>
            </Style>
         </CheckBox.Style>
      </CheckBox>
   </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

If you want to display the CheckBox only if a user hovers the row , meaning any cell of a row, you can use a RelativeSource binding to the IsMouseOver property the parent row.如果您只想在用户悬停该(即行的任何单元格)时显示CheckBox ,您可以使用RelativeSource绑定到父行的IsMouseOver属性。

<Style TargetType="{x:Type CheckBox}">
   <Setter Property="Visibility" Value="Hidden"/>
   <Style.Triggers>
      <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="True">
         <Setter Property="Visibility" Value="Visible"/>
      </DataTrigger>
   </Style.Triggers>
</Style>

If you want it to be dislayed only if a user hovers over the CheckBox column, your style will not work as you do not receive the mouse events on a hidden control.如果您希望它仅在用户将鼠标悬停在CheckBox列上时才显示,则您的样式将不起作用,因为您没有在隐藏控件上接收鼠标事件。 You can work around this with a Border that is visible.您可以使用可见的Border解决此问题。

<DataGridTemplateColumn>
   <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <Border x:Name="cbkBorder" Background="Transparent">
            <CheckBox Name="cbkSelect" 
                      IsChecked="{Binding Path=IsSelectedForOrder, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
               <CheckBox.Style>
                  <Style TargetType="CheckBox">
                     <Setter Property="Visibility" Value="Hidden"/>
                     <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=cbkBorder, Path=IsMouseOver}" Value="True">
                           <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                     </Style.Triggers>
                  </Style>
               </CheckBox.Style>
            </CheckBox>
         </Border>
      </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM