Friday, November 6, 2015

Generate a comma separated string from an IEnumerable object

string.join(",", Contacts.Select(x=>x.Name); //Name is a property of an object(Contact) in the list of Contacts

Wednesday, November 4, 2015

Simple yet effective way to find if IEnumerable has elements or not

bool IsEmpty(IEnumerable en)
{
    foreach(var c in en) { return false; }
    return true;
}

Monday, October 19, 2015

C# code to get numbers after decimal point

int decimalPoints=BitConverter.GetBytes(Decimal.GetBits(value)[3])[2];

Saturday, October 10, 2015

How do you force authors or template stylists to force add a control


How do you force authors or template stylists to force add a control
Solution- Provides template authors the information that these parts are required for styling this control -more like a contract

[TemplatePart [Name=TextBlockPart, Type =typeof(TextBlock)))

public class MyControl : Control
{

private const string TextBlockPart as PART_TextBlock;

TextBlock textblock;

protected TextBlock Textßlock

get { return textblock;}
set
{
if (textßlock is null)

(

textblock.Textlnput-= new TextCompositionEventHendler(textblockTextlnput)

J

Access TemplatedElements in CustomControl

How to access TemplatedElements in CustomControl?
Override OnApplyTemplate in the .cs file. Its called everytime a template is applied to a control

public override void OnApplyTenplate()
{
base.OnApplyTemplate();

var textblock= GetTemplateChild("Name Of Part") as TextBlock;

textblock.Text="hello";
}

How to get two way binding with Template Binding in WPF

Template Binding by default doesnt support Two Way binding.
If you still need to have it, here's the workaround:

<TextBox Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}"/>

Tuesday, August 18, 2015

Friday, May 15, 2015

WPF MultiBindng Converter example


Without Converter
<TextBlock.Text>
    <MultiBinding StringFormat="{}[{0} - {1}]">
        <Binding Path="Min" FallbackValue=""/>
        <Binding Path="Max" FallbackValue=""/>
    </MultiBinding>
</TextBlock.Text>
</TextBlock>


Using Converter
public class RangeMultiValueConvertor : IMultiValueConverter
   {
       public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
       {
           if (values == nullreturn string.Empty;
           if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue)
           {
               return string.Empty;
           }
           return (string.Format("[{0} - {1}]", values[0], values[1]));
       }
 
       public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
       {
           return null;
       }
   }

<local:RangeMultiValueConvertor x:Key="rangeConverter"/>

<TextBlock.Text>
    <MultiBinding  Converter="{StaticResource rangeConverter}">
        <Binding Path="Min"/>
        <Binding Path="Max"/>
    </MultiBinding>
</TextBlock.Text>

Wednesday, April 1, 2015

Solution for textBox text change not updating the view model/model

<TextBox Name="textBox1"
      Height="23" Width="463"
      HorizontalAlignment="Left" 
      Margin="12,12,0,0"   
      VerticalAlignment="Top"
      Text="{Binding OriginalText, UpdateSourceTrigger=PropertyChanged}" /> 
The TextBox.Text property has a default UpdateSourceTrigger value of LostFocus. This means if an application has a TextBox with a data-bound TextBox.Text property, the text you type into the TextBox does not update the source until the TextBox loses focus (for instance, when you click away from the TextBox).
If you want the source to get updated as you are typing, set the UpdateSourceTrigger of the binding to PropertyChanged. In the following example, the Text properties of both the TextBox and the TextBlock are bound to the same source property. The UpdateSourceTrigger property of the TextBox binding is set to PropertyChanged.

Serialization Exception: PropertyChangedEventManager is not serializable

Since events are non serializable, here is the way out to deal the problem when serializing model classes which have a property changed event to notify the view model classes:

http://www.primordialcode.com/blog/post/serialization-exception-propertychangedeventmanager-serializable

[NonSerialized]
private PropertyChangedEventHandler _PropertyChanged;
 
public virtual event PropertyChangedEventHandler PropertyChanged
{
  add { _PropertyChanged += value; }
  remove { _PropertyChanged -= value; }
}

Thursday, March 19, 2015

Remote Desktop Connection Shortcut

1. Right click on a empty area on desktop, and click on New and Shortcut.

2. Type the location below into the location area, and click on the Next button. (see screenshot below)
NOTE: Substitute ComputerName or IP.Address in the command below with the actual computer name or IP address of the computer that you want to make a RDC to.
mstsc.exe /v:ComputerName

OR

mstsc.exe /v:IP.Address

Thursday, February 19, 2015

Saturday, January 10, 2015

Introduction to REST and .net Web API

Friday, January 2, 2015

C# 6.0 New Features

http://www.kunal-chowdhury.com/2014/12/csharp-6-expression-bodied-method.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+kunal2383+%28Kunal%27s+Blog%29

Nice article on IDataErrorInfo and Validation in WPF

http://www.codeproject.com/Articles/858492/WPF-Validation-Using-IDataErrorInfo