如何在WPF的DataGrid中闪烁文字?


WPF程序中定义的一个DataGrid绑定报警数据,不同的报警等级数据显示的文字的颜色不同。比如1是红色,2是橙色等。

现在想把等级是1的文字进行闪烁(红色和白色),请问怎么实现。
我现在是这样定义xaml的


 <Grid>
        <Grid.Resources>
            <nxq_bal_convertor:ApplyBooleanShowConvertor x:Key="nxqApplyBooleanShowConvertor" ></nxq_bal_convertor:ApplyBooleanShowConvertor>
        </Grid.Resources>
        <TextBlock Text="查询的数据测试">
            <TextBlock.Resources>
            </TextBlock.Resources>
        </TextBlock>
        <DataGrid Name="dgBaseAlarmList" ItemsSource="{Binding}" IsReadOnly="True" SelectedCellsChanged="dgBaseAlarmList_SelectedCellsChanged"  AutoGenerateColumns="False" CanUserSortColumns="False" Margin="0,30,0,0" >
            <DataGrid.Resources>
                <Style TargetType="DataGridRow" x:Key="BlinkingStyle">
                    <Style.Setters>
                        <Setter Property="Background" Value="Red" />
                    </Style.Setters>
                    <Style.Triggers>
                        <Trigger Property="Visibility" Value="Visible">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard  BeginTime="00:00:00" 
                                        RepeatBehavior="Forever" 

                                        Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)">
                                        <ColorAnimation 
                                                    Duration="0:0:0.2"  From="White" To="Red" RepeatBehavior="Forever" AutoReverse="True"/>
                                    </Storyboard>   
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTextColumn Header="线路"  Binding="{Binding LineName}"/>
                <DataGridTextColumn Header="系统"  Binding="{Binding SystemName}"/>
                <DataGridTextColumn Header="车站"  Binding="{Binding StationName}"/>
                <DataGridTextColumn Header="日期"  Binding="{Binding AlarmTime}"/>
                <DataGridTextColumn Header="级别"  Binding="{Binding AlarmLevel}"/>
                <DataGridTextColumn Header="设备"  Binding="{Binding EqpName}"/>
                <DataGridTextColumn Header="属性描述"  Binding="{Binding EqpContext}"/>
                <DataGridTemplateColumn Header="确认" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Click="Button_Click" Content="{Binding Path=AlarmApply, Converter={StaticResource nxqApplyBooleanShowConvertor}}" IsEnabled="{Binding AlarmApply}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="确认时间"  Binding="{Binding AlarmApplyTime}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

然后是StyleSelector


 public class AlarmBlinkRowStyleSelector : StyleSelector
    {
        // Create a logger for use in this class
        private static readonly ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public Style BlinkingStyle { get; set; }

        public override Style SelectStyle(object item, DependencyObject container)
        {
            Alarm itemlocal = (Alarm)item;
            if (itemlocal.AlarmLevel == 1 && itemlocal._AlarmApply)//  100)
            {
                log.Info("进入AlarmBlinkRowStyleSelector: SelectStyle: BlinkingStyle" + ((Alarm)item).AlarmLevel);
                return this.BlinkingStyle;
            }
            else
            {
                log.Info("进入AlarmBlinkRowStyleSelector: SelectStyle: SelectStyle" + ((Alarm)item).AlarmLevel);
                return base.SelectStyle(item, container);
            }
        }

    }

在界面数据初始化之前


 this.dgBaseAlarmList.RowStyleSelector = new AlarmBlinkRowStyleSelector() { BlinkingStyle = (Style)this.dgBaseAlarmList.Resources["BlinkingStyle"] };

可是为什么无法改使文字颜色闪烁,甚至是前景色也无法变化。

请问怎么在datagrid中闪烁文字?

c# WPF datagrid

咆哮的柠檬 10 years, 9 months ago

Your Answer