问题描述
我有一个从数据库填充的WPF DataGrid。有一个细胞给我一个计数。如果它的值大于0,我想为该单元格添加一个闪烁的背景颜色。感谢您帮助我解决问题。p>创建一个转换器,检查一个单元格是否包含大于0的数字:
命名空间MyApp
{
public class GreaterThanZeroConverter:IValueConverter
{
public object Convert(object value,Type targetType,
object parameter,CultureInfo culture)
{
int cellValue ;
return Int32.TryParse((string)value,out cellValue)&& cellValue> 0;
public object ConvertBack(object value,Type targetType,
object parameter,CultureInfo culture)
{
return false;
}
}
}
将转换器名称空间包含在xaml。将 MyApp
替换为转换器的命名空间:
xmlns:myApp = clr-namespace:MyApp
您的网格必须看起来像这样。我绑定的对象有2个属性:Col1和Col2。如果Col1的值大于0,该单元格将闪烁红色。
< DataGrid ItemsSource ={Binding List} AutoGenerateColumns =False>
< DataGrid.Resources>
< myApp:GreaterThanZeroConverter
x:Key =GreaterThanZeroConverter>
< / myApp:GreaterThanZeroConverter>
< Style TargetType =DataGridCellx:Key =FlashStyle>
< Style.Triggers>
< DataTrigger
Binding ={Binding Col1,
Converter = {StaticResource GreaterThanZeroConverter}}
Value =True>
< DataTrigger.EnterActions>
< BeginStoryboard>
< Storyboard
x:Name =Blink
AutoReverse =True
RepeatBehavior =Forever>
< ColorAnimationUsingKeyFrames
BeginTime =00:00:00
Storyboard.TargetProperty =(Background)。(SolidColorBrush.Color)>
< EasingColorKeyFrame
KeyTime =00:00:01
Value =Red/>
< / ColorAnimationUsingKeyFrames>
< / Storyboard>
< / BeginStoryboard>
< /DataTrigger.EnterActions>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /DataGrid.Resources>
< DataGrid.Columns>
< DataGridTextColumn
Binding ={Binding Col1}
CellStyle ={StaticResource FlashStyle}>< / DataGridTextColumn>
< DataGridTextColumn
Binding ={Binding Col2}>< / DataGridTextColumn>
< /DataGrid.Columns>
< / DataGrid>
编辑
如果你必须多栏中的单元格基于它们包含的内容闪烁,可以更改
< DataTrigger
Binding ={Binding Col1,
Converter = {StaticResource GreaterThanZeroConverter}}
Value =True>
...
至
< DataTrigger
Binding ={Binding
Content.Text,
RelativeSource = {RelativeSource Self},
Converter = {StaticResource GreaterThanZeroConverter}}
Value =True>
并将您要闪烁的每个列的CellStyle设置为FlashStyle:
< DataGrid.Columns>
< DataGridTextColumn
Binding ={Binding Col1}
CellStyle ={StaticResource FlashStyle}>< / DataGridTextColumn>
< DataGridTextColumn
Binding ={Binding Col2}
CellStyle ={StaticResource FlashStyle}>< / DataGridTextColumn>
< /DataGrid.Columns>
请注意,这可能只适用于DataGridTextColumns。如果您正在使用DataGridTemplateColumns,将会更棘手一点。
I have a WPF DataGrid which is populated from a database. There is a cell which gives me a count. I want to add a flashing Background color to that cell if its value is more than 0. Thanks for helping me with my problem.
Create a converter that will check to see if a cell contains a number greater than 0:
namespace MyApp
{
public class GreaterThanZeroConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
int cellValue;
return Int32.TryParse((string)value, out cellValue) && cellValue > 0;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return false;
}
}
}
Include your converter namespace in the xaml. Replace MyApp
with the namespace of your converter:
xmlns:myApp="clr-namespace:MyApp"
Your grid will have to look something like this. The objects I am binding to have 2 properties: Col1 and Col2. If the value of Col1 is greater than 0, that cell will flash red.
<DataGrid ItemsSource="{Binding List}" AutoGenerateColumns="False">
<DataGrid.Resources>
<myApp:GreaterThanZeroConverter
x:Key="GreaterThanZeroConverter">
</myApp:GreaterThanZeroConverter>
<Style TargetType="DataGridCell" x:Key="FlashStyle">
<Style.Triggers>
<DataTrigger
Binding="{Binding Col1,
Converter={StaticResource GreaterThanZeroConverter}}"
Value="True" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard
x:Name="Blink"
AutoReverse="True"
RepeatBehavior="Forever">
<ColorAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)">
<EasingColorKeyFrame
KeyTime="00:00:01"
Value="Red" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn
Binding="{Binding Col1}"
CellStyle="{StaticResource FlashStyle}"></DataGridTextColumn>
<DataGridTextColumn
Binding="{Binding Col2}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Edit
If you have to make cells in multiple columns blink based on what they contain, you can change
<DataTrigger
Binding="{Binding Col1,
Converter={StaticResource GreaterThanZeroConverter}}"
Value="True" >
...
To
<DataTrigger
Binding="{Binding
Content.Text,
RelativeSource={RelativeSource Self},
Converter={StaticResource GreaterThanZeroConverter}}"
Value="True" >
And set the CellStyle for each of the columns that you want to blink to our FlashStyle:
<DataGrid.Columns>
<DataGridTextColumn
Binding="{Binding Col1}"
CellStyle="{StaticResource FlashStyle}"></DataGridTextColumn>
<DataGridTextColumn
Binding="{Binding Col2}"
CellStyle="{StaticResource FlashStyle}"></DataGridTextColumn>
</DataGrid.Columns>
Note that this will probably only work with DataGridTextColumns. If you are using DataGridTemplateColumns it will be a bit more tricky.
这篇关于如何满足某个数据条件,如何使DataGrid的Cell背景动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!