WPF Logical Resources

WPF Logical Resources
A Logical Resource in WPF is a XAML defined object that can be used by WPF elements inside the user interface.
You can declare a logical resource for the entire window and will be usable by all elements on that window, or you can declare it inside the Resources collection of an element, where it will be usable only within that element. You can also declare it for an entire application and it will usable anywhere.

       
              
              
       



       
            	
              	       
                      
            	
       



       
              
              
       


The application resources collection can be found inside the App.xaml file located in your project.

Every resource must have an x:Key property which defines the name of that specific object. It doesn’t have to be unique in the entire application, but it must be unique in the Resource Collection in which it was defined.

A resource in XAML can be accessed by using the {StaticResource name} syntax.


Mnemonic Keys in WPF

Mnemonic keys are keys that when pressed in combination with the ALT key move the focus to the control they are assigned to.

Labels in WPF have a built in support for mnemonic keys.

A mnemonic key can be specified by using the underscore symbol ( _ ) in front of the key you wish to assign. At runtime, you will notice that the key will appear underlined when the ALT key will be pressed.

Here is an example in XAML:



In this example, we have designated to Label1 a TextBox named TextBox1 and when the ALT+S keys will be pressed in combination, the textbox will receive focus.

SQL Banker's Rounding Function

The Banker's Rounding, also known as the Gaussian Rounding or Rounding to Even, rounds a given number if its fraction is half to the nearest even number.

The bellow SQL function rounds any given decimal number to a number of given digits and returns the rounded decimal number. It reads the number from right to left depending on the number of digits required and then rounds it. If the fraction is not half, then common rounding takes place.

CREATE FUNCTION [dbo].[BankersRounding]
(
	@value DECIMAL(28, 10),
	@digits INT
)

RETURNS DECIMAL (28, 10)
AS
BEGIN

	DECLARE @roundedValue DECIMAL(28, 10)
	DECLARE @roundingType INT 
	
		IF (ABS(@value - ROUND(@value, @digits, 1)) * POWER(10, @digits + 1) = 5)
			BEGIN
			
				IF (ROUND(ABS(@value) * POWER(10, @digits), 0, 1) % 2 = 0)
					BEGIN
						SET @roundingType = 1 
					END
				ELSE
					BEGIN
						SET @roundingType = 0
					END
						
				SET @roundedValue = ROUND(@value, @digits, @roundingType)
			END
				
		ELSE
			BEGIN
				SET @roundedValue = ROUND(@value, @digits)
			END
						
	RETURN @roundedValue
END

In C# there's the Round method inside the Math class that can take an enumeration overload called MidpointRounding.ToEven that does the exact same thing.

Here's an example:

using System;

namespace BankersRoundingProject
{
    public class BankersRounding
    {
        public static decimal GetRoundedValue(decimal value, int digits)
        {
            return Math.Round(value, digits, MidpointRounding.ToEven);
        }
    }
}