WPF 触发器实现 DataGrid 显示鼠标所在行自定义操作按钮

        <DataGrid Name="Viewer"       CanUserAddRows="False"
                                      BorderThickness="1" 
                                      BorderBrush="Gray" 
                  GridLinesVisibility="Horizontal"
                  
                                      AutoGenerateColumns="False"
                                      HorizontalAlignment="Left" 
                                      ItemsSource="{Binding AlertItems, Mode=TwoWay}"
                                      VerticalAlignment="Top" 
                                      Height="194" Width="482" 
                  Margin="173,87,0,0" 
                  >
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="60">
                    <DataGridTemplateColumn.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="告警颜色" />
                        </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="     " VerticalAlignment="Center" HorizontalAlignment="Center" Background="{Binding Color}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTextColumn  IsReadOnly="True"  ElementStyle="{StaticResource DataGridTextCenter}" Binding="{Binding LevelDescription, Mode=OneWay}" Header="优先级"></DataGridTextColumn>
                <DataGridTextColumn  IsReadOnly="True"  ElementStyle="{StaticResource DataGridTextCenter}" Binding="{Binding OperatorDescription, Mode=OneWay}" Header="比较符"></DataGridTextColumn>
                <DataGridTextColumn IsReadOnly="True" Width="50"    ElementStyle="{StaticResource DataGridTextCenter}" Binding="{Binding Ratio, StringFormat=0.00}" Header="百分比"/>
                <DataGridTextColumn  IsReadOnly="True"   ElementStyle="{StaticResource DataGridTextCenter}" Binding="{Binding Storage ,Converter={StaticResource B2SConverter}}" Width="35" Header="存储"></DataGridTextColumn>
                <DataGridTextColumn IsReadOnly="True" Width="180"   ElementStyle="{StaticResource DataGridTextCenter}" Binding="{Binding AlertText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="告警内容"/>

                    <DataGridTemplateColumn Header="" MinWidth="70">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <WrapPanel>
                                    <Button  Name="AlertLstEdit_btn" Visibility="Hidden"
                                        BorderThickness="0"
                                        Foreground="White" Click="AlertLstEdit_btn_Click">
                                        <Button.Background>
                                            <ImageBrush ImageSource="/PowerDiagram.Wpf;component/Images/Edit-icon.png" Stretch="Uniform"/>
                                        </Button.Background>
                                    </Button>
                                    <Button Name="AlertLstDel_btn" Margin="5,0,0,0" Visibility="Hidden"
                                        BorderThickness="0"
                                        Foreground="White" Click="AlertLstDel_btn_Click">
                                        <Button.Background>
                                            <ImageBrush Stretch="Uniform" ImageSource="/PowerDiagram.Wpf;component/Images/delete_2.png"/>
                                        </Button.Background>
                                    </Button>
                                </WrapPanel>
                                
                                
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}, Path=IsMouseOver}" Value="true">
                                        <Setter TargetName="AlertLstEdit_btn" Property="Visibility" Value="Visible" />
                                        <Setter TargetName="AlertLstDel_btn" Property="Visibility" Value="Visible" />
                                    </DataTrigger>
                                </DataTemplate.Triggers>                                
                                
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
            </DataGrid.Columns>
            <DataGrid.RowStyle>

                
            <Style TargetType="DataGridRow">
                <Setter Property="Height" Value="30" />
                <Style.Triggers>

                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="Background" Value="#FFFAFAFA" />
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="#BCF2F2F2" />
                    </Trigger>
                </Style.Triggers>
            </Style>                
                
            </DataGrid.RowStyle>
        </DataGrid>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值