Skip to content

Instantly share code, notes, and snippets.

@nairdo
Last active December 12, 2015 08:18
Show Gist options
  • Save nairdo/4743136 to your computer and use it in GitHub Desktop.
Save nairdo/4743136 to your computer and use it in GitHub Desktop.
Added simplicity to allow BadgeField users easier usage to color code by int value
// Example Usage:
<Rock:BadgeField DataField="FlagCount" HeaderText="Flag Count" SortExpression="FlagCount"
ImportantRuleEnable="true" ImportantMin="4"
WarningRuleEnable="true" WarningMin="2"
InfoRuleEnable="true" InfoMin="1" InfoMax="2" />
// Achieved via this in the BadgeField.cs...
// In the FormatDataMethod, if no SetBadgeType event hander is defined, then try
// setting the type based on "rules":
protected override string FormatDataValue( object dataValue, bool encode )
{
BadgeRowEventArgs eventArg = new BadgeRowEventArgs( dataValue );
if ( SetBadgeType != null )
{
SetBadgeType( this, eventArg );
}
else
{
// Try setting by rules...
SetBadgeTypeByRules( eventArg );
}
...
}
// The SetBadgeTypeByRules would do this:
private void SetBadgeTypeByRules( BadgeRowEventArgs e )
{
if ( !(e.FieldValue is int) )
return;
int count = (int)e.FieldValue;
if ( _importantRuleEnable && ( _importantMin <= count && count <= _importantMax ) )
{
e.BadgeType = BadgeType.Important;
}
else if ( _warningRuleEnable && ( _warningMin <= count && count <= _warningMax ) )
{
e.BadgeType = BadgeType.Warning;
}
else if ( _successRuleEnable && ( _successMin <= count && count <= _successMax ) )
{
e.BadgeType = BadgeType.Success;
}
else if ( _infoRuleEnable && ( _infoMin <= count && count <= _infoMax ) )
{
e.BadgeType = BadgeType.Info;
}
else
{
e.BadgeType = BadgeType.None;
}
}
// That would work because I would add the following properties to the BadgeField class:
/// <summary>
/// Gets or sets a value indicating whether the Important BadgeType rule is enabled.
/// </summary>
/// <value>
/// <c>true</c> if enabled; otherwise, <c>false</c>.
/// </value>
public string ImportantRuleEnable
{
set { bool.TryParse( value, out _importantRuleEnable ); }
}
private bool _importantRuleEnable = false;
/// <summary>
/// Gets or sets the important minimum value rule.
/// </summary>
/// <value>
/// The minimum value to be considered Important.
/// </value>
public string ImportantMin
{
set { int.TryParse( value, out _importantMin ); }
}
private int _importantMin = int.MinValue;
/// <summary>
/// Gets or sets the important max.
/// </summary>
/// <value>
/// The maximum value to be considered Important.
/// </value>
public string ImportantMax
{
set { int.TryParse( value, out _importantMax ); }
}
private int _importantMax = int.MaxValue;
/// <summary>
/// Gets or sets a value indicating whether the Warning BadgeType rule is enabled.
/// </summary>
/// <value>
/// <c>true</c> if enabled; otherwise, <c>false</c>.
/// </value>
public string WarningRuleEnable
{
set { bool.TryParse( value, out _warningRuleEnable ); }
}
private bool _warningRuleEnable = false;
/// <summary>
/// Gets or sets the Warning minimum value rule.
/// </summary>
/// <value>
/// The minimum value to be considered Warning.
/// </value>
public string WarningMin
{
set { int.TryParse( value, out _warningMin ); }
}
private int _warningMin = int.MinValue;
/// <summary>
/// Gets or sets the Warning max.
/// </summary>
/// <value>
/// The maximum value to be considered Warning.
/// </value>
public string WarningMax
{
set { int.TryParse( value, out _warningMax ); }
}
private int _warningMax = int.MaxValue;
/// <summary>
/// Gets or sets a value indicating whether the Success BadgeType rule is enabled.
/// </summary>
/// <value>
/// <c>true</c> if enabled; otherwise, <c>false</c>.
/// </value>
public string SuccessRuleEnable
{
set { bool.TryParse( value, out _successRuleEnable ); }
}
private bool _successRuleEnable = false;
/// <summary>
/// Gets or sets the Success minimum value rule.
/// </summary>
/// <value>
/// The minimum value to be considered Success.
/// </value>
public string SuccessMin
{
set { int.TryParse( value, out _successMin ); }
}
private int _successMin = int.MinValue;
/// <summary>
/// Gets or sets the Success max.
/// </summary>
/// <value>
/// The maximum value to be considered Success.
/// </value>
public string SuccessMax
{
set { int.TryParse( value, out _successMax ); }
}
private int _successMax = int.MaxValue;
/// <summary>
/// Gets or sets a value indicating whether the Info BadgeType rule is enabled.
/// </summary>
/// <value>
/// <c>true</c> if enabled; otherwise, <c>false</c>.
/// </value>
public string InfoRuleEnable
{
set { bool.TryParse( value, out _infoRuleEnable ); }
}
private bool _infoRuleEnable = false;
/// <summary>
/// Gets or sets the Info minimum value rule.
/// </summary>
/// <value>
/// The minimum value to be considered Info.
/// </value>
public string InfoMin
{
set { int.TryParse( value, out _infoMin ); }
}
private int _infoMin = int.MinValue;
/// <summary>
/// Gets or sets the Info max.
/// </summary>
/// <value>
/// The maximum value to be considered Info.
/// </value>
public string InfoMax
{
set { int.TryParse( value, out _infoMax ); }
}
private int _infoMax = int.MaxValue;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment