Created
March 18, 2012 16:23
-
-
Save bergerjac/2076769 to your computer and use it in GitHub Desktop.
LightSwitch: Strong Type FindControl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Client | |
using System; | |
using System.Diagnostics; | |
using System.Windows.Controls; | |
using Microsoft.LightSwitch.Presentation; | |
using Microsoft.LightSwitch.Presentation.Extensions; | |
namespace LightSwitchApplication | |
{ | |
public static class ControlHelpers | |
{ | |
public static void ActOnControl<TControl>( | |
this Microsoft.LightSwitch.Client.IScreenObject context, | |
string controlName, | |
Action<TControl> action) | |
where TControl : System.Windows.DependencyObject | |
{ | |
IContentItemProxy proxy = context.FindControl(controlName); | |
if (proxy == null) { return ; } | |
proxy.ControlAvailable += (o, e) => | |
{ | |
TControl control = e.Control as TControl; | |
if (control == null) { Debug.WriteLine("Wrong Control type."); return; } | |
action(control); | |
}; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Windows.Controls; | |
using Microsoft.LightSwitch; | |
namespace LightSwitchApplication | |
{ | |
public partial class ENTITYNew | |
{ | |
partial void ENTITYNew_InitializeDataWorkspace(List<IDataService> saveChangesTo) | |
{ | |
this.ENTITYProperty = new ENTITY(); | |
//this.FindControl("DateOf") ... no need for this any more | |
this.ActOnControl<DatePicker>( | |
this.ENTITYProperty.GetPropertyName(entity => entity.DateOf), | |
datePicker => datePicker.DisplayDateEnd = DateTime.Today); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Common | |
using System; | |
using System.Linq.Expressions; | |
using System.Reflection; | |
namespace LightSwitchApplication | |
{ | |
public static class Expressions | |
{ | |
///<summary>Gets a type's property name using an expression.</summary> | |
public static string GetPropertyName<T>(Expression<Func<T, object>> expression) | |
{ | |
PropertyInfo propertyInfo = GetPropertyInfo<T>(expression); | |
return propertyInfo.Name; | |
} | |
///<summary>Gets an object's property name using an expression.</summary> | |
public static string GetPropertyName<T>(this T t, Expression<Func<T, object>> expression) | |
{ | |
return GetPropertyName(expression); | |
} | |
private static PropertyInfo GetPropertyInfo<T>(Expression<Func<T, object>> expression) | |
{ | |
var lambda = expression as LambdaExpression; | |
MemberExpression memberExpression; | |
if (lambda.Body is UnaryExpression) | |
{ | |
UnaryExpression unaryExpression = lambda.Body as UnaryExpression; | |
memberExpression = unaryExpression.Operand as MemberExpression; | |
} | |
else | |
{ | |
memberExpression = lambda.Body as MemberExpression; | |
} | |
return memberExpression.Member as PropertyInfo; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment