Keep selection visual in RichEditBox on unfocus? - c#

Does someone know a way to keep the visual selection state of a selected text in a RichEditBox? I want to add some basic text editing to my Windows 8.1 App but every time I select a text and click on another UI Element within the app, the RichEditBox hides the selection.
I already tried to register the unfocus event an set the selection range again but unfortunately this has no effect.
I also tried to draw my own rect over the Text using
richEdit.Document.Selection.GetRect(PointOptions.ClientCoordinates,out selectionRect, out hitCount );
This works as long as only some text within a single line is selected. If the selection is multilined I only get the top-left of and the bottom-right position of the selected Text. It seems like these are the mouse Positions where the selection where started and where it ended.
Are there any other ways keep to the selected text visibible when the RichEditBox is unfocused.

I found another workaround. Just set the selection Background when the RichEditBox is unfocused. But Jerry's Post gave me the Inspiration to this solution. Seems like this way was to simple to find it a first place:
private void RichEditOnGotFocus(object sender, RoutedEventArgs routedEventArgs)
{
ITextSelection selectedText = richEdit.Document.Selection;
if (selectedText != null)
{
richEdit.Document.Selection.SetRange(_selectionStart, _selectionEnd);
selectedText.CharacterFormat.BackgroundColor = Colors.White;
}
}
private void RichEditOnLostFocus(object sender, RoutedEventArgs routedEventArgs)
{
_selectionEnd = richEdit.Document.Selection.EndPosition;
_selectionStart = richEdit.Document.Selection.StartPosition;
ITextSelection selectedText = richEdit.Document.Selection;
if (selectedText != null)
{
selectedText.CharacterFormat.BackgroundColor = Colors.Gray;
}
}

In the WinRT Toolkit, there's a HighlightBehavior that doesn't directly solve your problem, but might offer you a solution that is acceptable.
http://winrtxamltoolkit.codeplex.com/SourceControl/latest#WinRTXamlToolkit/WinRTXamlToolkit.Shared/Controls/Behaviors/HighlightBehavior.cs
public class HighlightBehavior : Behavior<TextBlock>
{
#region SearchString
/// <summary>
/// SearchString Dependency Property
/// </summary>
public static readonly DependencyProperty SearchStringProperty =
DependencyProperty.Register(
"SearchString",
typeof(string),
typeof(HighlightBehavior),
new PropertyMetadata(null, OnSearchStringChanged));
/// <summary>
/// Gets or sets the SearchString property. This dependency property
/// indicates the search string to highlight in the associated TextBlock.
/// </summary>
public string SearchString
{
get { return (string)GetValue(SearchStringProperty); }
set { SetValue(SearchStringProperty, value); }
}
/// <summary>
/// Handles changes to the SearchString property.
/// </summary>
/// <param name="d">
/// The <see cref="DependencyObject"/> on which
/// the property has changed value.
/// </param>
/// <param name="e">
/// Event data that is issued by any event that
/// tracks changes to the effective value of this property.
/// </param>
private static void OnSearchStringChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = (HighlightBehavior)d;
string oldSearchString = (string)e.OldValue;
string newSearchString = target.SearchString;
target.OnSearchStringChanged(oldSearchString, newSearchString);
}
/// <summary>
/// Provides derived classes an opportunity to handle changes
/// to the SearchString property.
/// </summary>
/// <param name="oldSearchString">The old SearchString value</param>
/// <param name="newSearchString">The new SearchString value</param>
private void OnSearchStringChanged(
string oldSearchString, string newSearchString)
{
UpdateHighlight();
}
#endregion
#region IsCaseSensitive
/// <summary>
/// IsCaseSensitive Dependency Property
/// </summary>
public static readonly DependencyProperty IsCaseSensitiveProperty =
DependencyProperty.Register(
"IsCaseSensitive",
typeof(bool),
typeof(HighlightBehavior),
new PropertyMetadata(false, OnIsCaseSensitiveChanged));
/// <summary>
/// Gets or sets the IsCaseSensitive property. This dependency property
/// indicates whether the highlight behavior is case sensitive.
/// </summary>
public bool IsCaseSensitive
{
get { return (bool)GetValue(IsCaseSensitiveProperty); }
set { SetValue(IsCaseSensitiveProperty, value); }
}
/// <summary>
/// Handles changes to the IsCaseSensitive property.
/// </summary>
/// <param name="d">
/// The <see cref="DependencyObject"/> on which
/// the property has changed value.
/// </param>
/// <param name="e">
/// Event data that is issued by any event that
/// tracks changes to the effective value of this property.
/// </param>
private static void OnIsCaseSensitiveChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = (HighlightBehavior)d;
bool oldIsCaseSensitive = (bool)e.OldValue;
bool newIsCaseSensitive = target.IsCaseSensitive;
target.OnIsCaseSensitiveChanged(oldIsCaseSensitive, newIsCaseSensitive);
}
/// <summary>
/// Provides derived classes an opportunity to handle changes
/// to the IsCaseSensitive property.
/// </summary>
/// <param name="oldIsCaseSensitive">The old IsCaseSensitive value</param>
/// <param name="newIsCaseSensitive">The new IsCaseSensitive value</param>
private void OnIsCaseSensitiveChanged(
bool oldIsCaseSensitive, bool newIsCaseSensitive)
{
UpdateHighlight();
}
#endregion
#region HighlightTemplate
/// <summary>
/// HighlightTemplate Dependency Property
/// </summary>
public static readonly DependencyProperty HighlightTemplateProperty =
DependencyProperty.Register(
"HighlightTemplate",
typeof(DataTemplate),
typeof(HighlightBehavior),
new PropertyMetadata(null, OnHighlightTemplateChanged));
/// <summary>
/// Gets or sets the HighlightTemplate property. This dependency property
/// indicates the template to use to generate the highlight Run inlines.
/// </summary>
public DataTemplate HighlightTemplate
{
get { return (DataTemplate)GetValue(HighlightTemplateProperty); }
set { SetValue(HighlightTemplateProperty, value); }
}
/// <summary>
/// Handles changes to the HighlightTemplate property.
/// </summary>
/// <param name="d">
/// The <see cref="DependencyObject"/> on which
/// the property has changed value.
/// </param>
/// <param name="e">
/// Event data that is issued by any event that
/// tracks changes to the effective value of this property.
/// </param>
private static void OnHighlightTemplateChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = (HighlightBehavior)d;
DataTemplate oldHighlightTemplate = (DataTemplate)e.OldValue;
DataTemplate newHighlightTemplate = target.HighlightTemplate;
target.OnHighlightTemplateChanged(oldHighlightTemplate, newHighlightTemplate);
}
/// <summary>
/// Provides derived classes an opportunity to handle changes
/// to the HighlightTemplate property.
/// </summary>
/// <param name="oldHighlightTemplate">The old HighlightTemplate value</param>
/// <param name="newHighlightTemplate">The new HighlightTemplate value</param>
private void OnHighlightTemplateChanged(
DataTemplate oldHighlightTemplate, DataTemplate newHighlightTemplate)
{
UpdateHighlight();
}
#endregion
#region HighlightBrush
/// <summary>
/// HighlightBrush Dependency Property
/// </summary>
public static readonly DependencyProperty HighlightBrushProperty =
DependencyProperty.Register(
"HighlightBrush",
typeof(Brush),
typeof(HighlightBehavior),
new PropertyMetadata(new SolidColorBrush(Colors.Red), OnHighlightBrushChanged));
/// <summary>
/// Gets or sets the HighlightBrush property. This dependency property
/// indicates the brush to use to highlight the found instances of the search string.
/// </summary>
/// <remarks>
/// Note that the brush is ignored if HighlightTemplate is specified
/// </remarks>
public Brush HighlightBrush
{
get { return (Brush)GetValue(HighlightBrushProperty); }
set { SetValue(HighlightBrushProperty, value); }
}
/// <summary>
/// Handles changes to the HighlightBrush property.
/// </summary>
/// <param name="d">
/// The <see cref="DependencyObject"/> on which
/// the property has changed value.
/// </param>
/// <param name="e">
/// Event data that is issued by any event that
/// tracks changes to the effective value of this property.
/// </param>
private static void OnHighlightBrushChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = (HighlightBehavior)d;
Brush oldHighlightBrush = (Brush)e.OldValue;
Brush newHighlightBrush = target.HighlightBrush;
target.OnHighlightBrushChanged(oldHighlightBrush, newHighlightBrush);
}
/// <summary>
/// Provides derived classes an opportunity to handle changes
/// to the HighlightBrush property.
/// </summary>
/// <param name="oldHighlightBrush">The old HighlightBrush value</param>
/// <param name="newHighlightBrush">The new HighlightBrush value</param>
private void OnHighlightBrushChanged(
Brush oldHighlightBrush, Brush newHighlightBrush)
{
UpdateHighlight();
}
#endregion
private PropertyChangeEventSource<string> _textChangeEventSource;
/// <summary>
/// Called after the behavior is attached to an AssociatedObject.
/// </summary>
/// <remarks>
/// Override this to hook up functionality to the AssociatedObject.
/// </remarks>
protected override void OnAttached()
{
UpdateHighlight();
_textChangeEventSource = new PropertyChangeEventSource<string>(this.AssociatedObject, "Text", BindingMode.OneWay);
_textChangeEventSource.ValueChanged += TextChanged;
base.OnAttached();
}
/// <summary>
/// Called when the behavior is being detached from its AssociatedObject, but
/// before it has actually occurred.
/// </summary>
/// <remarks>
/// Override this to unhook functionality from the AssociatedObject.
/// </remarks>
protected override void OnDetaching()
{
ClearHighlight();
_textChangeEventSource.ValueChanged -= TextChanged;
_textChangeEventSource = null;
base.OnDetaching();
}
private void TextChanged(object sender, string s)
{
UpdateHighlight();
}
/// <summary>
/// Updates the highlight.
/// </summary>
public void UpdateHighlight()
{
if (this.AssociatedObject == null ||
string.IsNullOrEmpty(this.AssociatedObject.Text) ||
string.IsNullOrEmpty(this.SearchString))
{
ClearHighlight();
return;
}
var txt = this.AssociatedObject.Text;
var searchTxt = this.SearchString;
var processedCharacters = 0;
this.AssociatedObject.Inlines.Clear();
int pos;
while ((pos = txt.IndexOf(
searchTxt,
processedCharacters,
this.IsCaseSensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase)) >= 0)
{
if (pos > processedCharacters)
{
var run = new Run
{
Text =
txt.Substring(
processedCharacters, pos - processedCharacters)
};
this.AssociatedObject.Inlines.Add(run);
}
Run highlight;
var highlightText = txt.Substring(pos, searchTxt.Length);
if (this.HighlightTemplate == null)
{
highlight =
new Run
{
Text = highlightText,
Foreground = this.HighlightBrush
};
}
else
{
highlight = (Run)this.HighlightTemplate.LoadContent();
highlight.Text = highlightText;
}
this.AssociatedObject.Inlines.Add(highlight);
processedCharacters = pos + searchTxt.Length;
}
if (processedCharacters < txt.Length)
{
var run = new Run
{
Text =
txt.Substring(
processedCharacters, txt.Length - processedCharacters)
};
this.AssociatedObject.Inlines.Add(run);
}
}
/// <summary>
/// Clears the highlight.
/// </summary>
public void ClearHighlight()
{
if (this.AssociatedObject == null)
{
return;
}
var text = this.AssociatedObject.Text;
this.AssociatedObject.Inlines.Clear();
this.AssociatedObject.Inlines.Add(new Run{Text = text});
}
}
public abstract class Behavior<T> : Behavior where T : DependencyObject
{
#region Behavior() - CTOR
/// <summary>
/// Initializes a new instance of the <see cref="Behavior<T>"/> class.
/// </summary>
protected Behavior()
{
_associatedType = typeof(T);
}
#endregion
#region AssociatedObject
/// <summary>
/// Gets the object to which this <see cref="Behavior<T>" /> is attached.
/// </summary>
public new T AssociatedObject
{
get
{
return (T)_associatedObject;
}
internal set
{
_associatedObject = value;
}
}
#endregion
}
Best of luck!

Related

Unity: Singleton DontDestroyOnLoad Build Error

I am using XMGUnityLib Plugin. The program runs fine in the editor without any error but when I try to build it (Android) then while at pushing apk step it gives this error:
InvalidOperationException: The following game object is invoking the DontDestroyOnLoad method: AnalyticsManager. Notice that DontDestroyOnLoad can only be used in play mode and, as such, cannot be part of an editor script.
XMGSingleton`1[XMGAnalyticsManager].get_Instance () (at Assets/XMG/UnityLib/Util/XMGSingleton.cs:28)
AndroidStorePostProcess.OnPostprocessScene () (at Assets/Editor/AndroidStorePostProcess.cs:32)
UnityEditor.HostView:OnGUI()
XMGSingleton.cs:
using UnityEngine;
using System.Collections;
using System;
/// <summary>
/// XMG singleton base Class.
/// Inherit your Manager Monobehavior classes that you want to be Singletons
/// </summary>
public abstract class XMGSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
protected static T instance;
/// <summary>
/// Returns the instance of this singleton
/// Make sure we don't get destroyed on Scene Load
/// </summary>
public static T Instance {
get {
if(instance == null) {
instance = (T) FindObjectOfType(typeof(T));
if (instance == null) {
Debug.LogError("An instance of " + typeof(T) +
" is needed in the scene, but there is none.");
return null;
}
DontDestroyOnLoad(instance.gameObject);
}
return instance;
}
}
#region Public Methods
public static bool HasInstance {
get {
if( (T) FindObjectOfType(typeof(T)) != null ) {
return true;
} else {
return false;
}
}
}
/// <summary>
/// Helper to cast the Instance to the Type provided
/// </summary>
public static Type MyInstance<Type> () {
return (Type)(object)Instance;
}
#endregion
}
XMGAnalyticsManager.cs:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Com.XMG.UnityLib.Community.OnlineGame;
using Com.XMG.UnityLib.Tracking;
#if UNITY_IPHONE
using FlurryAccess = FlurryBinding;
#elif UNITY_ANDROID
using FlurryAccess = FlurryAndroid;
#endif
public abstract partial class XMGAnalyticsManager : XMGSingleton<XMGAnalyticsManager> {
#region Private Fields
/// <summary>
/// The type of the current build.
/// For Analytics QA testing point to Analytics
/// For QA Testing point to SandBox
/// For Production/Release build point to Production
/// </summary>
[SerializeField]
private BuildTypeEnum buildType = BuildTypeEnum.SandBox;
/// <summary>
/// The type of the android store type
/// </summary>
[SerializeField]
private AndroidStoreTypeEnum androidStoreType = AndroidStoreTypeEnum.Google;
/// <summary>
/// The swrve authentication Info
/// </summary>
[SerializeField]
private SwrveAuthentication swrveAuth;
/// <summary>
/// The flurry auth.
/// </summary>
[SerializeField]
private FlurryAuthentication flurryAuth;
/// <summary>
/// Sends events every X seconds.
/// </summary>
[SerializeField]
private float sendEventsEverySeconds = 30;
/// <summary>
/// The parent screen.
/// </summary>
[SerializeField]
private string parentScreen = "SplashPage";
/// <summary>
/// The grand parent screen.
/// </summary>
string grandParentScreen = "null";
/// <summary>
/// My external IP.
/// </summary>
string myExtIP = "";
/// <summary>
/// The swrve component.
/// </summary>
private SwrveComponent swrveComponent;
private Dictionary<string, JSONObject> swrveResources = new Dictionary<string, JSONObject>();
/// <summary>
/// Resource hashmap ----> (key: resource name, value: json object)
/// </summary>
public Dictionary<string, JSONObject> Resources {
get {
return this.swrveResources;
}
}
/// <summary>
/// This event is triggered when swrve data is updated.
/// </summary>
public static event Action ItemsConfigurationUpdatedEvent = delegate {};
/// <summary>
/// True if we are in the fetching of AB Test Data State
/// </summary>
private bool isFetchingABParams = false;
/// <summary>
/// The current swrve identifier.
/// </summary>
private string currentSwrveId = null;
/// <summary>
/// The android bundle version.
/// </summary>
[SerializeField]
protected string androidBundleVersion = "1.0";
#endregion
#region Public Properties
/// <summary>
/// True if it's a production build
/// </summary>
public bool IsProductionBuild {
get { return buildType == BuildTypeEnum.Production; }
}
/// <summary>
/// Gets the type of the android store.
/// </summary>
public AndroidStoreTypeEnum AndroidStoreType {
get { return androidStoreType; }
set { androidStoreType = value; }
}
/// <summary>
/// True if its a google play store build
/// </summary>
public bool IsGoogleStoreType {
get { return androidStoreType == AndroidStoreTypeEnum.Google; }
}
/// <summary>
/// True if its an Amazon store build
/// </summary>
public bool IsAmazonStoreType {
get { return androidStoreType == AndroidStoreTypeEnum.Amazon; }
}
#endregion
#region Monobehaviors & Init
/// <summary>
/// Add event Listeners
/// </summary>
void Awake() {
XMGOnlineProfileManager.OnProfileLoadedEvent += ProfileHasLoadedCallback;
XMGCommunityManager.OnCreateRealProfileOnServerEvent += ProfileCreatedOnServerCallback;
XMGCommunityManager.OnNewFriendsAddedEvent += NewFriendsAddedCallback;
RegisterGameListeners();
// TODO: Listen for Profile Change events
swrveAuth.Init();
flurryAuth.Init();
}
/// <summary>
/// Starts the SWRVE Event timer
/// </summary>
void Start() {
StartSwrveTimer();
StartCoroutine( CheckIP() );
}
/// <summary>
/// Checks for the IP
/// </summary>
/// <returns>
/// The IP
/// </returns>
IEnumerator CheckIP(){
WWW myExtIPWWW = new WWW("http://checkip.dyndns.org");
if( myExtIPWWW != null ) {
yield return myExtIPWWW;
if( myExtIPWWW.error == null ) {
myExtIP=myExtIPWWW.text;
myExtIP=myExtIP.Substring(myExtIP.IndexOf(":")+1);
myExtIP=myExtIP.Substring(0,myExtIP.IndexOf("<"));
}
}
}
/// <summary>
/// Detach event Listeners
/// </summary>
void OnDestroy() {
XMGOnlineProfileManager.OnProfileLoadedEvent -= ProfileHasLoadedCallback;
XMGCommunityManager.OnCreateRealProfileOnServerEvent -= ProfileCreatedOnServerCallback;
XMGCommunityManager.OnNewFriendsAddedEvent -= NewFriendsAddedCallback;
UnRegisterGameListeners();
}
/// <summary>
/// If Pause end session
/// If Resume Start session and Re-Login.
/// At any case post events
/// </summary>
/// <param name='pause'>
/// Pause.
/// </param>
void OnApplicationPause(bool pause) {
if (currentSwrveId != null) {
if(pause) {
swrveComponent.Swrve_AddSessionEnd();
} else {
swrveComponent.Swrve_AddSessionStart();
SessionStart();
AnalyticsLogout();
AnalyticsLogin();
}
LogUserProperties();
swrveComponent.Swrve_PostEvents(currentSwrveId, SWRVE_APP_VERSION);
}
}
/// <summary>
/// Init plugins.The Profile Has loaded
/// </summary>
void ProfileHasLoadedCallback() {
// Get Swrve component
swrveComponent = gameObject.GetComponentInChildren<SwrveComponent>();
AnalyticsLogin();
// Check if we've set the random user property, if not then set it
if (!PlayerPrefs.HasKey(RANDOM_TEST_PROPERTY_KEY)) {
PlayerPrefs.SetString(RANDOM_TEST_PROPERTY_KEY, "true");
JSONObject userProperty = new JSONObject(JSONObject.Type.OBJECT);
int random = NGUITools.RandomRange(1, 100);
userProperty.AddField("RandNum", random.ToString());
swrveComponent.Swrve_AddUserUpdateEvent(userProperty.ToString());
}
}
/// <summary>
/// A new Online Profile created callback.
/// </summary>
void ProfileCreatedOnServerCallback() {
AddNamedEvent( "Community.FacebookUserCreated" );
}
/// <summary>
/// New friends were added callback.
/// </summary>
/// <param name='newFriendsAdded'>
/// The New friends added
/// </param>
void NewFriendsAddedCallback( int newFriendsAdded ) {
JSONObject jTournamentFriends = new JSONObject(JSONObject.Type.OBJECT);
jTournamentFriends.AddField( "NewFriendsAdded", BucketGeneral( newFriendsAdded ) );
AddNamedEvent ( "Community.TournamentAddedFriend", jTournamentFriends );
}
#endregion
#region Abstract & Virtual Methods
/// <summary>
/// Gets the tracking Object
/// </summary>
protected abstract XMGTracking GetTracking();
/// <summary>
/// Registers the game listeners.
/// </summary>
protected abstract void RegisterGameListeners();
/// <summary>
/// Unregister the game listeners.
/// </summary>
protected abstract void UnRegisterGameListeners();
/// <summary>
/// Logs the game user properties.
/// </summary>
/// <param name='jUserProperties'>
/// The JSON Object with the user properties.
/// </param>
protected abstract void LogGameUserProperties(JSONObject jUserProperties);
/// <summary>
/// A session Is Started
/// </summary>
protected abstract void SessionStart();
/// <summary>
/// The session has ended
/// </summary>
protected abstract void SessionEnd();
/// <summary>
/// Gets the user start time.
/// Default to Now
/// Override to provide the user's start time
/// </summary>
/// <returns>
/// The user start time.
/// </returns>
protected virtual long GetUserStartTime() {
return (long)XMGUnityUtil.ToUnixTimeFromUniversal( DateTime.UtcNow );
}
#endregion
#region Private Methods
/// <summary>
/// Starts the swrve timer.
/// </summary>
private void StartSwrveTimer() {
StartCoroutine( StartEventsTimer());
}
/// <summary>
/// Logs the user properties.
/// And Calls abstract LogGameUserProperties
/// </summary>
private void LogUserProperties() {
JSONObject jUserProperties = new JSONObject(JSONObject.Type.OBJECT);
// Add here Common User Properties
XMGTracking tracking = GetTracking();
jUserProperties.AddField( UP_FIRST_LAUNCH_DAY, tracking.FirstLaunch.Day );
jUserProperties.AddField( UP_FIRST_LAUNCH_MONTH, tracking.FirstLaunch.Month );
jUserProperties.AddField( UP_FIRST_LAUNCH_YEAR, tracking.FirstLaunch.Year );
jUserProperties.AddField( UP_FIRST_LAUNCH_EPOCH, XMGUnityUtil.ToUnixTimeFromUniversal( tracking.FirstLaunch ).ToString() );
jUserProperties.AddField( UP_TOTAL_TAPJOY_REWARDS, tracking.TotalTapJoyPointsEarned );
jUserProperties.AddField( UP_TOTAL_SPONSORPAY_REWARDS, tracking.TotalSponsorPayPointsEarned );
jUserProperties.AddField( UP_TOTAL_CHARTBOOST_ADS_CLICKED, tracking.TotalChartBoostAdsClicked );
jUserProperties.AddField( UP_TOTAL_CHARTBOOST_ADS_CLOSED, tracking.TotalChartBoostAdsClosed );
jUserProperties.AddField( UP_TOTAL_SOFT_CURRENCY_EARNED, tracking.SoftCurrencyEarned );
jUserProperties.AddField( UP_TOTAL_HARD_CURRENCY_EARNED, tracking.HardCurrencyEarned );
jUserProperties.AddField( UP_TOTAL_SOFT_CURRENCY_SPENT, tracking.SoftCurrencySpent );
jUserProperties.AddField( UP_TOTAL_HARD_CURRENCY_SPENT, tracking.HardCurrencySpent );
#if UNITY_IPHONE
jUserProperties.AddField( UP_VERION, EtceteraTwoBinding.getInfoPlistValue("CFBundleVersion") );
#elif UNITY_ANDROID
jUserProperties.AddField( UP_VERION, androidBundleVersion );
#endif
jUserProperties.AddField( UP_TOTAL_FRIENDS, tracking.TotalFriends );
jUserProperties.AddField( UP_TOTAL_UNIQUE_DAYS_PLAYED, tracking.TotalUniqueDaysPlayed );
if( string.IsNullOrEmpty( myExtIP ) ) {
jUserProperties.AddField( UP_IP, myExtIP );
}
LogGameUserProperties(jUserProperties);
swrveComponent.Swrve_AddUserUpdateEvent(jUserProperties.ToString());
}
#endregion
#region Authentication
/// <summary>
/// Logs in to Flurry and SWRVE
/// </summary>
void AnalyticsLogin() {
FlurryLogin();
if( currentSwrveId != null ) {
if( !currentSwrveId.Equals( XMGOnlineProfileManager.Instance.ActiveOnlineProfile.GameStats.SwrveId ) ) {
Debug.Log("There is already a swrve account signed in!!! You must close the session first!");
AnalyticsLogout();
} else {
// This swrve user is already logged in, bail out of this function
Debug.Log("Attempt to log into swrve twice with the same id... Ignoring.");
return;
}
}
SwrveLogin(XMGOnlineProfileManager.Instance.ActiveOnlineProfile.GameStats.SwrveId);
}
/// <summary>
/// Logout from Flurry and SWRVE
/// </summary>
private void AnalyticsLogout() {
FlurryLogout();
if(currentSwrveId == null) {
Debug.LogError("There is no active swrve user!!! Cannot close session.");
return;
}
SwrveLogout();
}
/// <summary>
/// Starts Flurry Session
/// </summary>
private void FlurryLogin() {
#if UNITY_IPHONE
FlurryAccess.startSession(flurryAuth.FlurryKey);
#elif UNITY_ANDROID
FlurryAccess.onStartSession(flurryAuth.FlurryKey, true, true );
#endif
}
/// <summary>
/// Logouts from Flurry
/// </summary>
private void FlurryLogout() {
#if UNITY_ANDROID
FlurryAccess.onEndSession();
#endif
}
/// <summary>
/// Login to Swrve
/// </summary>
/// <param name='swrveId'>
/// The Swrve identifier.
/// </param>
private void SwrveLogin(string swrveId) {
currentSwrveId = swrveId;
int gameId = 0;
string apiKey = "";
if (buildType == BuildTypeEnum.Analytics) {
gameId = swrveAuth.AnalyticsGameID;
apiKey = swrveAuth.AnalyticsAPIKey;
} else if (buildType == BuildTypeEnum.SandBox) {
gameId = swrveAuth.SandboxGameID;
apiKey = swrveAuth.SandboxAPIKey;
} else if( buildType == BuildTypeEnum.Production ) {
gameId = swrveAuth.ProductionGameID;
apiKey = swrveAuth.ProductionAPIKey;
}
InitializeSWRVEComponent(gameId,
apiKey,
swrveAuth.PersonalKey,
swrveAuth.URLABTest
);
}
/// <summary>
/// Logout from Swrve
/// </summary>
private void SwrveLogout() {
// Make sure to close the Swrve session
SessionEnd();
swrveComponent.Swrve_AddSessionEnd();
swrveComponent.Swrve_PostEvents(currentSwrveId, SWRVE_APP_VERSION);
currentSwrveId = null;
}
/// <summary>
/// Initializes the SWRVE component.
/// </summary>
/// <param name='gameId'>
/// The Game identifier.
/// </param>
/// <param name='apiKey'>
/// The API key.
/// </param>
/// <param name='personalKey'>
/// Personal key.
/// </param>
/// <param name='abURL'>
/// The Ab Test server URL.
/// </param>
private void InitializeSWRVEComponent(int gameId, string apiKey, string personalKey, string abURL) {
swrveComponent.gameId = gameId;
swrveComponent.apiKey = apiKey;
swrveComponent.abTestServer = abURL;
// Setup swrve for new user
swrveComponent.Swrve_AddSessionStart();
SessionStart();
if(isFetchingABParams == false) {
isFetchingABParams = true;
StartCoroutine(GetAllResourcesWithABTests());
}
}
#endregion
#region A/B Helpers
/// <summary>
/// Returns the most recent JSON configuration for a resource from swrve or NULL if there is none.
/// This means it could return cached data that is old. If you want to retrieve the latest from swrve.
/// </summary>
/// <param name="itemId">
/// A <see cref="System.String"/>
/// </param>
/// <returns>
/// A <see cref="JSONObject"/>
/// </returns>
public JSONObject GetItemParams(string itemId) {
JSONObject item = null;
swrveResources.TryGetValue(itemId, out item);
return item;
}
/// <summary>
/// Gets all resources with AB tests.
/// </summary>
/// <returns>
/// The all resources with AB tests.
/// </returns>
private IEnumerator GetAllResourcesWithABTests() {
string swrveURLRequest = string.Format(swrveAuth.URLABTestResources + "?api_key={0}&user={1}&joined={2}", swrveComponent.apiKey, currentSwrveId, GetUserStartTime() );
WWW itemRequest = new WWW(swrveURLRequest);
yield return itemRequest; // Yield until a result is returned.
if(itemRequest.error != null) {
Debug.LogWarning("Error attempting to fetch Swrve A/B Resource data: " + itemRequest.error);
LoadCachedConfig();
// Bail!
yield break;
}
// Process all the resources into a hashmap ( key: resource name, value: json object )
JSONObject jResources = new JSONObject(itemRequest.text);
if(jResources == null || jResources.type == JSONObject.Type.NULL) {
// Bad data from swrve, abort!
Debug.LogError("Bad A/B resource data from swrve!");
LoadCachedConfig();
yield break;
} else {
//Debug.LogWarning( "Data from SWRVE: " + jResources );
}
XMGSaveLoadUtils.Instance.SaveEncryptedField(CACHED_CONFIG_KEY, itemRequest.text); // Replace the old cached config.
ParseResources(jResources);
if( swrveResources.Count > 0 ) {
ItemsConfigurationUpdatedEvent();
}
isFetchingABParams = false;
}
/// <summary>
/// Loads the cached swrve config file
/// </summary>
private void LoadCachedConfig() {
string cached = XMGSaveLoadUtils.Instance.LoadEncryptedField(CACHED_CONFIG_KEY);
if(string.IsNullOrEmpty(cached)) {
// There was no cached data
return;
}
// Apply the cached resources so it doesn't block updates.
JSONObject jResources = new JSONObject(cached);
ParseResources(jResources);
if( swrveResources.Count > 0 ) {
ItemsConfigurationUpdatedEvent();
}
}
/// <summary>
/// Expects a JSON array of resources.
/// </summary>
/// <param name="resources">
/// A <see cref="JSONObject"/>
/// </param>
private void ParseResources(JSONObject jResources) {
swrveResources.Clear();
//Debug.Log( "Parsing SWRVE Resources: " + jResources );
if(jResources == null || jResources.type != JSONObject.Type.ARRAY) {
// Bad data from swrve, abort!
Debug.LogError("Could not parse resource data, unexpected format!");
return;
}
foreach(JSONObject data in jResources.list) {
JSONObject key = data.GetField("uid");
if(key == null || key.type != JSONObject.Type.STRING || string.IsNullOrEmpty( JSONObject.GetString(key) ) ) {
// Bad item, on to the next
Debug.LogWarning("Bad item, no property 'uid' in " + data.print());
continue;
}
// Add the resource over top of any precached configuration
swrveResources[JSONObject.GetString(key)] = data;
}
}
#endregion
#region Event Handling
/// <summary>
/// Adds the named event.
/// </summary>
/// <param name='name'>
/// The EventName.
/// </param>
public void AddNamedEvent(string name) {
swrveComponent.Swrve_AddNamedEvent(name, "{}");
#if UNITY_IPHONE
FlurryAccess.logEvent(name, false);
#elif UNITY_ANDROID
FlurryAccess.logEvent(name);
#endif
}
/// <summary>
/// Adds a named event with Payload
/// </summary>
/// <param name='name'>
/// The EventName.
/// </param>
/// <param name='payload'>
/// Payload.
/// </param>
public void AddNamedEvent(string name, JSONObject payload) {
string data = payload.ToString();
if(!string.IsNullOrEmpty(data)) {
swrveComponent.Swrve_AddNamedEvent(name, data);
} else {
swrveComponent.Swrve_AddNamedEvent(name, "{}");
}
#if UNITY_IPHONE
FlurryAccess.logEventWithParameters(name, payload.ToDictionary(), false);
#elif UNITY_ANDROID
FlurryAccess.logEvent(name, payload.ToDictionary());
#endif
}
/// <summary>
/// Infinite Loop to send events in the queue
/// </summary>
private IEnumerator StartEventsTimer() {
float timeInterval = Time.realtimeSinceStartup;
while (true) {
if(Time.realtimeSinceStartup - timeInterval >= sendEventsEverySeconds) {
SendEvents();
timeInterval = Time.realtimeSinceStartup;
}
yield return new WaitForSeconds( 1 );
}
}
/// <summary>
/// Sends the events to SWRVE
/// </summary>
private void SendEvents() {
if(currentSwrveId != null) {
swrveComponent.Swrve_PostEvents(currentSwrveId, SWRVE_APP_VERSION);
}
}
#endregion
#region UI Events
/// <summary>
/// Logs a UI screen visit event.
/// Records the current screen, the parent screen, and the grandparent screen.
/// </summary>
/// <param name='screenName'>
/// Screen name.
/// </param>
public void ScreenVisitEvent(string screenName) {
JSONObject eventParams = new JSONObject(JSONObject.Type.OBJECT);
if(parentScreen != null) {
eventParams.AddField("parent", parentScreen);
}
if(grandParentScreen != null) {
eventParams.AddField("grandParent", grandParentScreen);
}
AddNamedEvent("UI.Flow." + screenName, eventParams);
eventParams.AddField("screen", screenName);
AddNamedEvent("UI.Flow.Screens", eventParams);
grandParentScreen = parentScreen;
parentScreen = screenName;
}
/// <summary>
/// Logs a UI button pressed event
/// </summary>
/// <param name='buttonName'>
/// The Button name.
/// </param>
public void ButtonPressedEvent( string buttonName ) {
AddNamedEvent("UI.ButtonPressed." + buttonName );
}
#endregion
#region Purchase Events
/// <summary>
/// Sends an analytics buy in event for purchasing inapps.
/// </summary>
/// <param name='rewardCurrency'>
/// Reward currency
/// </param>
/// <param name='rewardAmount'>
/// Amount of rewardCurrency
/// </param>
/// <param name='localCost'>
/// The real money price in local currency ( e.g 0.99 )
/// </param>
/// <param name='localCurrency'>
/// CAD, USD, etc...
/// </param>
public void AddBuyInEvent(string rewardCurrency, int rewardAmount, float localCost, string localCurrency, string itemID ) {
swrveComponent.Swrve_AddBuyInEvent( "", rewardCurrency, rewardAmount, Mathf.Round(localCost* 100)/100, localCurrency );
AddNamedEvent( "Purhases.IAP" );
AddNamedEvent( "Purhases.IAP.HardCurrency" + itemID );
}
/// <summary>
/// Analytics event when a user purchases an item in-game.
/// </summary>
/// <param name='itemID'>
/// The UID for the Item
/// </param>
/// <param name='currency'>
/// The currency
/// </param>
/// <param name='itemCost'>
/// Item cost.
/// </param>
/// <param name='itemQuantity'>
/// Item quantity.
/// </param>
public void AddPurchaseEvent( string itemID, string currency, int itemCost, int itemQuantity ) {
swrveComponent.Swrve_AddPurchaseItemEvent( itemID, currency, itemCost, itemQuantity );
}
/// <summary>
/// Adds a purchase conversion event.
/// </summary>
/// <param name='itemID'>
///The unique Item ID
/// </param>
public void AddPurchaseConversionPackEvent( string itemID ) {
AddNamedEvent( "Purchases.HardCurrency.SoftCurrency" );
AddNamedEvent( "Purhases.HardCurrency.SoftCurrency." + itemID );
}
#endregion
}
Is this singleton class correctly coded? If yes, what could be the problem?
Any help will be appreciated.
Thanks :)
Do you have an "Editor Script" that inherits from this class? If so then most probably this is what's causing the error. If you're not sure what this means, try looking for a folder in your project named Editor and looking in its scripts. Or try searching for any scripts that are using UnityEditor; and that inherit from Editor like this: public class Name : Editor and make them not inherit from this class.
Another solution is try commenting the DontDestroyOnLoad line (line 26 in XMGSingleton.cs) and put it yourself where you need it.
Go to the objects that you're using this script with and do something like this:
void Awake()
{
if (instance == null)
{
DontDestroyOnLoad(gameObject);
instance = this;
}
else
if (instance != this)
Destroy(gameObject);
}
Though the post is a bit old, I recently bumped into this issue. It was a bit confusing because I had not added any new editor script and other editor scripts were not being called from the Singleton GameObject that the error was pointing to.
When I tried restarting Unity and then again created the build, it started giving some other exception.
However, I finally found the issue was being caused by a method from another object (created by the Singleton's OnAwake method) trying to access an uninitialized property of the Singleton (because the OnAwake method execution was not completed).
I fixed it by delaying the other object's method callback based on a different event.

How to create DataGridTextColumn change event in WPF(MVVM)?

How to create DataGridTextColumn change event in WPF(MVVM)?
Thanks
You can bind a command to DataGrid's CellEditEnding event:
<DataGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellEditEnding">
<ec:EventToCommand PassEventArgsToCommand="True"
Command="{Binding ItemEditedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
And in your codebehind
private ICommand _ItemEditedCommand;
public ICommand ItemEditedCommand => _ItemEditedCommand ?? (_ItemEditedCommand = new RelayCommand<DataGridCellEditEndingEventArgs>(ItemEditedCommand_Execute));
private void ItemEditedCommand_Execute(object param)
{
var cell = param as DataGridCellEditEndingEventArgs;
// Examine cell column and row and act accordingly
}
You don't necessarily need to use EventToCommand you can just use whatever you normally use inside the event trigger.
EDIT:
Here is an implementation of RelayCommand and EventToCommand in case you're having problems finding them:
EventToCommand:
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
namespace MyProj.Helpers.Command
{
/// <summary>
/// This <see cref="T:System.Windows.Interactivity.TriggerAction`1" /> can be
/// used to bind any event on any FrameworkElement to an <see cref="ICommand" />.
/// Typically, this element is used in XAML to connect the attached element
/// to a command located in a ViewModel. This trigger can only be attached
/// to a FrameworkElement or a class deriving from FrameworkElement.
/// <para>To access the EventArgs of the fired event, use a RelayCommand<EventArgs>
/// and leave the CommandParameter and CommandParameterValue empty!</para>
/// </summary>
////[ClassInfo(typeof(EventToCommand),
//// VersionString = "5.2.8",
//// DateString = "201504252130",
//// Description = "A Trigger used to bind any event to an ICommand.",
//// UrlContacts = "http://www.galasoft.ch/contact_en.html",
//// Email = "laurent#galasoft.ch")]
public class EventToCommand : TriggerAction<DependencyObject>
{
/// <summary>
/// Identifies the <see cref="CommandParameter" /> dependency property
/// </summary>
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
"CommandParameter",
typeof(object),
typeof(EventToCommand),
new PropertyMetadata(
null,
(s, e) =>
{
var sender = s as EventToCommand;
if (sender == null)
{
return;
}
if (sender.AssociatedObject == null)
{
return;
}
sender.EnableDisableElement();
}));
/// <summary>
/// Identifies the <see cref="Command" /> dependency property
/// </summary>
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
"Command",
typeof(ICommand),
typeof(EventToCommand),
new PropertyMetadata(
null,
(s, e) => OnCommandChanged(s as EventToCommand, e)));
/// <summary>
/// Identifies the <see cref="MustToggleIsEnabled" /> dependency property
/// </summary>
public static readonly DependencyProperty MustToggleIsEnabledProperty = DependencyProperty.Register(
"MustToggleIsEnabled",
typeof(bool),
typeof(EventToCommand),
new PropertyMetadata(
false,
(s, e) =>
{
var sender = s as EventToCommand;
if (sender == null)
{
return;
}
if (sender.AssociatedObject == null)
{
return;
}
sender.EnableDisableElement();
}));
private object _commandParameterValue;
private bool? _mustToggleValue;
/// <summary>
/// Gets or sets the ICommand that this trigger is bound to. This
/// is a DependencyProperty.
/// </summary>
public ICommand Command
{
get
{
return (ICommand)GetValue(CommandProperty);
}
set
{
SetValue(CommandProperty, value);
}
}
/// <summary>
/// Gets or sets an object that will be passed to the <see cref="Command" />
/// attached to this trigger. This is a DependencyProperty.
/// </summary>
public object CommandParameter
{
get
{
return GetValue(CommandParameterProperty);
}
set
{
SetValue(CommandParameterProperty, value);
}
}
/// <summary>
/// Gets or sets an object that will be passed to the <see cref="Command" />
/// attached to this trigger. This property is here for compatibility
/// with the Silverlight version. This is NOT a DependencyProperty.
/// For databinding, use the <see cref="CommandParameter" /> property.
/// </summary>
public object CommandParameterValue
{
get
{
return _commandParameterValue ?? CommandParameter;
}
set
{
_commandParameterValue = value;
EnableDisableElement();
}
}
/// <summary>
/// Gets or sets a value indicating whether the attached element must be
/// disabled when the <see cref="Command" /> property's CanExecuteChanged
/// event fires. If this property is true, and the command's CanExecute
/// method returns false, the element will be disabled. If this property
/// is false, the element will not be disabled when the command's
/// CanExecute method changes. This is a DependencyProperty.
/// </summary>
public bool MustToggleIsEnabled
{
get
{
return (bool)GetValue(MustToggleIsEnabledProperty);
}
set
{
SetValue(MustToggleIsEnabledProperty, value);
}
}
/// <summary>
/// Gets or sets a value indicating whether the attached element must be
/// disabled when the <see cref="Command" /> property's CanExecuteChanged
/// event fires. If this property is true, and the command's CanExecute
/// method returns false, the element will be disabled. This property is here for
/// compatibility with the Silverlight version. This is NOT a DependencyProperty.
/// For databinding, use the <see cref="MustToggleIsEnabled" /> property.
/// </summary>
public bool MustToggleIsEnabledValue
{
get
{
return _mustToggleValue == null
? MustToggleIsEnabled
: _mustToggleValue.Value;
}
set
{
_mustToggleValue = value;
EnableDisableElement();
}
}
/// <summary>
/// Called when this trigger is attached to a FrameworkElement.
/// </summary>
protected override void OnAttached()
{
base.OnAttached();
EnableDisableElement();
}
#if SILVERLIGHT
private Control GetAssociatedObject()
{
return AssociatedObject as Control;
}
#else
/// <summary>
/// This method is here for compatibility
/// with the Silverlight version.
/// </summary>
/// <returns>The FrameworkElement to which this trigger
/// is attached.</returns>
private FrameworkElement GetAssociatedObject()
{
return AssociatedObject as FrameworkElement;
}
#endif
/// <summary>
/// This method is here for compatibility
/// with the Silverlight 3 version.
/// </summary>
/// <returns>The command that must be executed when
/// this trigger is invoked.</returns>
private ICommand GetCommand()
{
return Command;
}
/// <summary>
/// Specifies whether the EventArgs of the event that triggered this
/// action should be passed to the bound RelayCommand. If this is true,
/// the command should accept arguments of the corresponding
/// type (for example RelayCommand<MouseButtonEventArgs>).
/// </summary>
public bool PassEventArgsToCommand
{
get;
set;
}
/// <summary>
/// Gets or sets a converter used to convert the EventArgs when using
/// <see cref="PassEventArgsToCommand"/>. If PassEventArgsToCommand is false,
/// this property is never used.
/// </summary>
public IEventArgsConverter EventArgsConverter
{
get;
set;
}
/// <summary>
/// The <see cref="EventArgsConverterParameter" /> dependency property's name.
/// </summary>
public const string EventArgsConverterParameterPropertyName = "EventArgsConverterParameter";
/// <summary>
/// Gets or sets a parameters for the converter used to convert the EventArgs when using
/// <see cref="PassEventArgsToCommand"/>. If PassEventArgsToCommand is false,
/// this property is never used. This is a dependency property.
/// </summary>
public object EventArgsConverterParameter
{
get
{
return GetValue(EventArgsConverterParameterProperty);
}
set
{
SetValue(EventArgsConverterParameterProperty, value);
}
}
/// <summary>
/// Identifies the <see cref="EventArgsConverterParameter" /> dependency property.
/// </summary>
public static readonly DependencyProperty EventArgsConverterParameterProperty = DependencyProperty.Register(
EventArgsConverterParameterPropertyName,
typeof(object),
typeof(EventToCommand),
new PropertyMetadata(null));
/// <summary>
/// The <see cref="AlwaysInvokeCommand" /> dependency property's name.
/// </summary>
public const string AlwaysInvokeCommandPropertyName = "AlwaysInvokeCommand";
/// <summary>
/// Gets or sets a value indicating if the command should be invoked even
/// if the attached control is disabled. This is a dependency property.
/// </summary>
public bool AlwaysInvokeCommand
{
get
{
return (bool)GetValue(AlwaysInvokeCommandProperty);
}
set
{
SetValue(AlwaysInvokeCommandProperty, value);
}
}
/// <summary>
/// Identifies the <see cref="AlwaysInvokeCommand" /> dependency property.
/// </summary>
public static readonly DependencyProperty AlwaysInvokeCommandProperty = DependencyProperty.Register(
AlwaysInvokeCommandPropertyName,
typeof(bool),
typeof(EventToCommand),
new PropertyMetadata(false));
/// <summary>
/// Provides a simple way to invoke this trigger programatically
/// without any EventArgs.
/// </summary>
public void Invoke()
{
Invoke(null);
}
/// <summary>
/// Executes the trigger.
/// <para>To access the EventArgs of the fired event, use a RelayCommand<EventArgs>
/// and leave the CommandParameter and CommandParameterValue empty!</para>
/// </summary>
/// <param name="parameter">The EventArgs of the fired event.</param>
protected override void Invoke(object parameter)
{
if (AssociatedElementIsDisabled()
&& !AlwaysInvokeCommand)
{
return;
}
var command = GetCommand();
var commandParameter = CommandParameterValue;
if (commandParameter == null
&& PassEventArgsToCommand)
{
commandParameter = EventArgsConverter == null
? parameter
: EventArgsConverter.Convert(parameter, EventArgsConverterParameter);
}
if (command != null
&& command.CanExecute(commandParameter))
{
command.Execute(commandParameter);
}
}
private static void OnCommandChanged(
EventToCommand element,
DependencyPropertyChangedEventArgs e)
{
if (element == null)
{
return;
}
if (e.OldValue != null)
{
((ICommand)e.OldValue).CanExecuteChanged -= element.OnCommandCanExecuteChanged;
}
var command = (ICommand)e.NewValue;
if (command != null)
{
command.CanExecuteChanged += element.OnCommandCanExecuteChanged;
}
element.EnableDisableElement();
}
private bool AssociatedElementIsDisabled()
{
var element = GetAssociatedObject();
return AssociatedObject == null
|| (element != null
&& !element.IsEnabled);
}
private void EnableDisableElement()
{
var element = GetAssociatedObject();
if (element == null)
{
return;
}
var command = GetCommand();
if (MustToggleIsEnabledValue
&& command != null)
{
element.IsEnabled = command.CanExecute(CommandParameterValue);
}
}
private void OnCommandCanExecuteChanged(object sender, EventArgs e)
{
EnableDisableElement();
}
}
/// <summary>
/// The definition of the converter used to convert an EventArgs
/// in the <see cref="EventToCommand"/> class, if the
/// <see cref="EventToCommand.PassEventArgsToCommand"/> property is true.
/// Set an instance of this class to the <see cref="EventToCommand.EventArgsConverter"/>
/// property of the EventToCommand instance.
/// </summary>
////[ClassInfo(typeof(EventToCommand))]
public interface IEventArgsConverter
{
/// <summary>
/// The method used to convert the EventArgs instance.
/// </summary>
/// <param name="value">An instance of EventArgs passed by the
/// event that the EventToCommand instance is handling.</param>
/// <param name="parameter">An optional parameter used for the conversion. Use
/// the <see cref="EventToCommand.EventArgsConverterParameter"/> property
/// to set this value. This may be null.</param>
/// <returns>The converted value.</returns>
object Convert(object value, object parameter);
}
}
RelayCommand:
using System;
using System.Diagnostics;
using System.Windows.Input;
namespace MyProj.Helpers.Command
{
public class RelayCommand : ICommand
{
#region Properties
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
#endregion
#region Constructors
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null) throw new ArgumentNullException(nameof(execute));
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter) => _canExecute == null || _canExecute(parameter);
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion // ICommand Members }
}
public class RelayCommand<T> : ICommand
{
#region Fields
readonly Action<T> _execute;
readonly Predicate<T> _canExecute;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of <see cref="DelegateCommand{T}"/>.
/// </summary>
/// <param name="execute">Delegate to execute when Execute is called on the command. This can be null to just hook up a CanExecute delegate.</param>
/// <remarks><seealso cref="CanExecute"/> will always return true.</remarks>
public RelayCommand(Action<T> execute)
: this(execute, null)
{
}
/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<T> execute, Predicate<T> canExecute)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
///<summary>
///Defines the method that determines whether the command can execute in its current state.
///</summary>
///<param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
///<returns>
///true if this command can be executed; otherwise, false.
///</returns>
public bool CanExecute(object parameter) => _canExecute?.Invoke((T)parameter) ?? true;
///<summary>
///Occurs when changes occur that affect whether or not the command should execute.
///</summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
///<summary>
///Defines the method to be called when the command is invoked.
///</summary>
///<param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to <see langword="null" />.</param>
public void Execute(object parameter)
{
_execute((T)parameter);
}
#endregion
}
}

Image visibility stopped working in Windows Phone 8.1 after xamarin.forms upgrade

In my xamarin forms app, On main page a list of items are displayed. A checkbox image is made visible when user selects an item from the list. Image visibility is toggled when user taps on an item, to give user the impression that item has been selected.
This functionality was working fine in all platforms i.e. iOs, Android, WP8.1. But once when I upgraded my xamarin forms version from 1.5X to 2.X, The checkbox image stoped appearing in windows phone when user select an item. Although in iOs and android its working fine.
If I downgrade the xamarin forms back to version 1.5, functionality works fine.
I also discovered that when app starts in windows phone, If we rotate the screen horizontally, the checkbox image starts functioning again. But that's just a hack!!!
For the checkbox functionality I have used the following code -
/// <summary>
/// A selectable cross-platform cell
/// </summary>
public class CustomSelectableCell : ViewCell
{
private Grid _gridLayout;
private Label _text;
private Image _image;
/// <summary>
/// The BindableProperty
/// </summary>
public static readonly BindableProperty CommandProperty = BindableProperty.Create<CustomSelectableCell, Command>(i => i.Command, default(Command));
/// <summary>
/// Gets/Sets the command which gets executed on tapped
/// </summary>
public Command Command
{
get
{
return (Command)this.GetValue(CommandProperty);
}
set
{
this.SetValue(CommandProperty, value);
}
}
/// <summary>
/// Then BindableProperty
/// </summary>
public static readonly BindableProperty AutoSelectProperty = BindableProperty.Create<CustomSelectableCell, bool>(i => i.AutoSelect, true, BindingMode.TwoWay);
/// <summary>
/// Gets/Sets if the cell will automatically select the itself by tapping
/// </summary>
public bool AutoSelect
{
get
{
return (bool)this.GetValue(AutoSelectProperty);
}
set
{
this.SetValue(AutoSelectProperty, value);
}
}
/// <summary>
/// The BindableProperty
/// </summary>
public static readonly BindableProperty IsSelectedProperty = BindableProperty.Create<CustomSelectableCell, bool>(i => i.IsSelected, default(bool), BindingMode.TwoWay, null, IsSelectedChanged);
/// <summary>
/// Gets/Sets if the cell is in selected state or not
/// </summary>
public bool IsSelected
{
get
{
return (bool)this.GetValue(IsSelectedProperty);
}
set
{
this.SetValue(IsSelectedProperty, value);
}
}
/// <summary>
/// The BindableProperty
/// </summary>
public static readonly BindableProperty TextProperty = BindableProperty.Create<CustomSelectableCell, string>(i => i.Text, default(string), BindingMode.TwoWay, null, TextChanged);
/// <summary>
/// Gets/Sets the text of the cell
/// </summary>
public string Text
{
get
{
return (string)this.GetValue(TextProperty);
}
set
{
this.SetValue(TextProperty, value);
}
}
/// <summary>
/// Creates a new instance of <c>CustomSelectableCell</c>
/// </summary>
public CustomSelectableCell()
{
this._gridLayout = new Grid
{
RowDefinitions =
{
new RowDefinition
{
Height = new GridLength(1, GridUnitType.Star)
}
},
ColumnDefinitions =
{
new ColumnDefinition
{
Width = new GridLength(1, GridUnitType.Star)
},
new ColumnDefinition
{
Width = new GridLength(1, GridUnitType.Auto)
}
}
};
this._image = new Image
{
Source = "ic_done_white_24dp.png",
IsVisible = false
};
this._text = new Label
{
Style = Device.Styles.ListItemTextStyle
};
this._gridLayout.Children.Add(this._text, 0, 0);
this._gridLayout.Children.Add(this._image, 1, 0);
this.View = this._gridLayout;
}
/// <summary>
/// Raised when <c>IsSelected</c> changed
/// </summary>
/// <param name="obj">The cell</param>
/// <param name="oldValue">Old value</param>
/// <param name="newValue">New value</param>
private static void IsSelectedChanged(BindableObject obj, bool oldValue, bool newValue)
{
var cell = obj as CustomSelectableCell;
if (cell != null)
{
cell._image.IsVisible = newValue;
}
}
/// <summary>
/// Raised when the <c>Text</c> changed
/// </summary>
/// <param name="obj">The cell</param>
/// <param name="oldValue">Old value</param>
/// <param name="newValue">New value</param>
private static void TextChanged(BindableObject obj, string oldValue, string newValue)
{
var cell = obj as CustomSelectableCell;
if (cell != null)
{
cell._text.Text = newValue;
}
}
/// <summary>
/// Auto Select on tapped
/// </summary>
protected override void OnTapped()
{
base.OnTapped();
if (this.AutoSelect)
{
this.IsSelected = !this.IsSelected;
}
if (this.Command != null)
{
if (this.Command.CanExecute(this))
{
this.Command.Execute(this);
}
}
}
}
Could someone please help it get resolved??? Any help would be greatly appreciated. Please let me know if you need more information.
Thanks.
Jkm

Custom C# button's DialogResult doesn't work correctly on modal form

Having a little trouble making a Winforms ImageButton class (C#). My ImageButton class implements the IButtonControl interface but when I add it to a form, and set the button's DialogResult to 'OK' , and then call ShowDialog() on the form, pressing the button does not close the form and return the DialogResult, as the normal Winforms Button control does.
Here's my implementation of ImageButton, feel free to do whatever you like with it.
/// <summary>
/// A control that displays an image and responds to mouse clicks on the image.
/// </summary>
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[Designer(typeof(ImageButtonDesigner))]
[ToolboxItem(typeof(ImageButtonToolboxItem))]
public class ImageButton : PictureBox, IButtonControl, INotifyPropertyChanged
{
#region Constructors
/// <summary>
/// Initializes a new instance of the ImageButton class using the default initial values.
/// </summary>
public ImageButton()
{
DoubleBuffered = true;
BackColor = Color.Transparent;
SizeMode = PictureBoxSizeMode.AutoSize;
}
#endregion
#region Properties
/// <summary>
/// Backing field for the DialogResult property.
/// </summary>
private DialogResult dialogResult;
/// <summary>
/// Gets or sets a value that is returned to the parent form when the button is clicked.
/// </summary>
[Category("Behavior")]
[Description("The dialog-box result produced in a modal form by clicking the button")]
public DialogResult DialogResult
{
get
{
return dialogResult;
}
set
{
if (Enum.IsDefined(typeof(DialogResult), value))
dialogResult = value;
else
throw new InvalidEnumArgumentException("value", (int)value, typeof(DialogResult));
}
}
/// <summary>
/// Backing field for the Idle property.
/// </summary>
private Image idle;
/// <summary>
/// The image that will be displayed on the control when the mouse is not over a visible part of it.
/// </summary>
[Category("Appearance")]
[Description("The image that will be displayed on the Control when the mouse is not over a visible part of it.")]
public Image Idle
{
get
{
return idle;
}
set
{
idle = value;
NotifyPropertyChanged();
}
}
/// <summary>
/// Backing field for the Mouseover property
/// </summary>
private Image mouseover;
/// <summary>
/// The image that will be displayed on the control when the mouse is over a visible part of it.
/// </summary>
[Category("Appearance")]
[Description("The image that will be displayed on the control when the mouse is over a visible part of it.")]
public Image Mouseover
{
get { return mouseover; }
set
{
mouseover = value;
NotifyPropertyChanged();
}
}
/// <summary>
/// Backing field for the Mousedown property
/// </summary>
private Image mousedown;
/// <summary>
/// The image that will be displayed on the control when the left mouse button is held down and the mouse is over a visible part of it.
/// </summary>
[Category("Appearance")]
[Description("The image that will be displayed on the control when the left mouse button is held down and the mouse is over a visible part of it.")]
public Image Mousedown
{
get { return mousedown; }
set
{
mousedown = value;
NotifyPropertyChanged();
}
}
/// <summary>
/// Gets or sets the text associated with the control.
/// </summary>
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Category("Appearance")]
[Description("The text associated with the control.")]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
/// <summary>
/// Gets or sets the font of the text displayed by the control.
/// </summary>
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Category("Appearance")]
[Description("The font used to display text in the control.")]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
}
}
/// <summary>
/// Gets or sets the image that is displayed by the PictureBox.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
[Category("Appearance")]
[Description("The image displayed in the PictureBox.")]
new public Image Image
{
get
{
return base.Image;
}
set
{
base.Image = value;
}
}
/// <summary>
/// Backing field for the ButtonState property.
/// </summary>
private ButtonStates buttonState = ButtonStates.None;
/// <summary>
/// The current state of the button.
/// </summary>
private ButtonStates ButtonState
{
get { return buttonState; }
set
{
buttonState = value;
NotifyPropertyChanged();
}
}
/// <summary>
/// Gets the default size of the control.
/// </summary>
protected override Size DefaultSize
{
get
{
return new Size(75, 23);
}
}
#endregion
#region Enums
/// <summary>
/// Specifies the current state of a button.
/// </summary>
[Flags]
private enum ButtonStates : byte
{
/// <summary>
///
/// </summary>
[Description("")]
None = 0,
/// <summary>
///
/// </summary>
[Description("")]
Default = 1 << 0,
/// <summary>
///
/// </summary>
[Description("")]
Mouseover = 1 << 1,
/// <summary>
///
/// </summary>
[Description("")]
Mousedown = 1 << 2
}
#endregion
#region Events
/// <summary>
/// Occurs when a property value changes.
/// </summary>
[Category("Property Changed")]
[Description("Occurs when a property value changes.")]
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Methods
/// <summary>
/// Raises the System.ComponentModel.PropertyChanged event.
/// </summary>
/// <param name="propertyName">the name of the property that changed.</param>
protected virtual void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
if (propertyName == "Idle")
Image = Idle;
}
/// <summary>
/// Notifies the button whether it is the default button so that it can adjust its appearance accordingly.
/// </summary>
/// <param name="value">true if the button is to have the appearance of the default button; otherwise, false.</param>
public void NotifyDefault(bool value)
{
ButtonState = value ? ButtonState | ButtonStates.Default : ButtonState & ~ButtonStates.Default;
}
/// <summary>
/// Generates a Click event for a button.
/// </summary>
public void PerformClick()
{
if (CanSelect)
{
OnClick(EventArgs.Empty);
}
}
/// <summary>
/// Raises the System.Windows.Control.TextChanged event.
/// </summary>
/// <param name="e">A System.EventArgs that contains the event data.</param>
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
Refresh();
}
/// <summary>
/// Raises the System.Windows.Forms.Paint event.
/// </summary>
/// <param name="pe">A PaintEventArgs that contains the event data.</param>
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if ((!string.IsNullOrEmpty(Text)) && (pe != null) && (base.Font != null))
{
SolidBrush drawBrush = new SolidBrush(base.ForeColor);
// Calculate the size of the text that will be drawn onto the control.
SizeF drawStringSize = pe.Graphics.MeasureString(base.Text, base.Font);
// The registration point used to draw the text.
PointF drawPoint;
if (base.Image != null)
drawPoint = new PointF(base.Image.Width / 2 - drawStringSize.Width / 2, base.Image.Height / 2 - drawStringSize.Height / 2);
else
drawPoint = new PointF(base.Width / 2 - drawStringSize.Width / 2, base.Height / 2 - drawStringSize.Height / 2);
pe.Graphics.DrawString(base.Text, base.Font, drawBrush, drawPoint);
}
}
/// <summary>
/// Raises the System.Windows.Forms.MouseEnter event.
/// </summary>
/// <param name="e">A System.EventArgs that contains the event data.</param>
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
ButtonState |= ButtonStates.Mouseover;
Image = Mouseover;
}
/// <summary>
/// Raises the System.Windows.Forms.MouseLeave event.
/// </summary>
/// <param name="e">A System.EventArgs that contains the event data.</param>
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
ButtonState &= ~ButtonStates.Mouseover;
Image = Idle;
}
/// <summary>
/// Raises the System.Windows.Forms.MouseDown event.
/// </summary>
/// <param name="e">A System.Windows.Forms.MouseEventArgs that contains the event data.</param>
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
ButtonState |= ButtonStates.Mousedown;
Image = Mousedown;
}
/// <summary>
/// Raises the System.Windows.Forms.MouseUp event.
/// </summary>
/// <param name="e">A System.Windows.Forms.MouseEventArgs that contains the event data.</param>
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
ButtonState &= ~ButtonStates.Mousedown;
Image = ((ButtonState & ButtonStates.Mouseover) != 0) ? Mouseover : idle;
}
#endregion
}
[Serializable]
internal class ImageButtonToolboxItem : ToolboxItem
{
public ImageButtonToolboxItem() : base(typeof(ImageButton)) { }
protected ImageButtonToolboxItem(SerializationInfo info, StreamingContext context)
{
Deserialize(info, context);
}
protected override IComponent[] CreateComponentsCore(IDesignerHost host)
{
ImageButton imageButton = (ImageButton)host.CreateComponent(typeof(ImageButton));
Assembly assembly = Assembly.GetAssembly(typeof(ImageButton));
using (Stream streamMouseover = assembly.GetManifestResourceStream("Mvc.Mouseover.png"))
using (Stream streamMousedown = assembly.GetManifestResourceStream("Mvc.Mousedown.png"))
using (Stream streamIdle = assembly.GetManifestResourceStream("Mvc.Idle.png"))
{
imageButton.Idle = Image.FromStream(streamIdle);
imageButton.Mouseover = Image.FromStream(streamMouseover);
imageButton.Mousedown = Image.FromStream(streamMousedown);
}
return new IComponent[] { imageButton };
}
}
internal class ImageButtonDesigner : ControlDesigner
{
protected override void PostFilterAttributes(System.Collections.IDictionary attributes)
{
base.PostFilterAttributes(attributes);
Attribute dockingBehaviour = new DockingAttribute(DockingBehavior.Never);
attributes[typeof(DockingAttribute)] = dockingBehaviour;
}
public override SelectionRules SelectionRules
{
get
{
return SelectionRules.Moveable;
}
}
}
Sorry about the messy toolbox item and designer code..
My Question is, Does there need to be anything special implementation wise to get a button control to work on a modal form (in the same way the normal button does)
(NB. The Form's BorderStyle property is set to None, don't know if thats important)
Thanks, in advance!
You need to actually apply the assigned DialogResult property, it is not automatic. Do so by overriding the OnClick() method:
protected override void OnClick(EventArgs e) {
var form = this.FindForm();
if (form != null) form.DialogResult = dialogResult;
base.OnClick(e);
}
Technically you should also notify accessibility clients of the state change, it isn't clear at all whether you care about this. It tends to be skipped for custom controls but you generally ought to. Insert these statements before the base.OnClick() call:
base.AccessibilityNotifyClients(AccessibleEvents.StateChange, -1);
base.AccessibilityNotifyClients(AccessibleEvents.NameChange, -1);

CheckedListBox and Binding Object Properties to Items

I'm curious if there is a simple way to bind properties of a class to individual items of a CheckedListBox. There are several components out there (Telerik/DevExpress) that provide PropertyEditor grids, but I'm looking to do it in a CheckedListBox.
IE:
public class MyItem
{
public bool Property1
{
get;
set;
}
public bool Property2
{
get;
set;
}
public bool Property3
{
get;
set;
}
}
And when adding items to the CheckedListBox, I'd like to have some sort of method that lets me do:
this.AddCheckListBoxItem("Property A", this.myItem.Property1);
this.AddCheckListBoxItem("Property B", this.myItem.Property2);
this.AddCheckListBoxItem("Property C", this.myItem.Property3);
the first parameter being the display name within the CheckedListBox.
Then throughout any changes to the checkstate, the bool values would automatically be updated without further code.
Currently there isn't any easy/simple way to get the functionallity you are looking for.
As in the comments the nearest solution I can think of would be to use reflection.
If you manage to build a helper class that has this functionallity, please post here as I would also find that usefull.
I've created a solution using reflection as suggested by #Jethro. I used generics in the class definition, but I didn't implement anything that uses them yet. To summarize, basically using reflection, I've bound a single object and it's boolean properties to individual items within a standard CheckedListBox. It's nice as it doesn't require having to code to set the property values and get them when saving data, as the reflection takes care of this.
I created a Form, and added a CheckedListBox to it, and a button. Here's the code side.
using System;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
/// <summary>
/// The main form.
/// </summary>
public partial class Form1 : Form
{
/// <summary>
/// The checked list box property helper.
/// </summary>
private CheckedListBoxPropertyHelper<MyItem> helper;
/// <summary>
/// My object to bind to the checked list box.
/// </summary>
private MyItem myObjectDataSource;
/// <summary>
/// Initializes a new instance of the <see cref="Form1"/> class.
/// </summary>
public Form1()
{
this.InitializeComponent();
this.myObjectDataSource = new MyItem();
this.helper = new CheckedListBoxPropertyHelper<MyItem>(this.checkedListBox1, this.myObjectDataSource, true);
this.helper.AddCheckListBoxItem(new CheckedPropertyItem("Property One", "Property1"));
this.helper.AddCheckListBoxItem(new CheckedPropertyItem("Property Two", "Property2"));
this.helper.AddCheckListBoxItem(new CheckedPropertyItem("Property Three", "Property3"));
}
/// <summary>
/// Handles the Click event of the Button1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void Button1_Click(object sender, EventArgs e)
{
// In the constructor
// if we instantiated: this.helper = new CheckedListBoxPropertyHelper<MyItem>(this.checkedListBox1, this.myObjectDataSource, true);
// as: this.helper = new CheckedListBoxPropertyHelper<MyItem>(this.checkedListBox1, this.myObjectDataSource, false);
// changing the last bindImmediate property to false, the changes to the checkboxes wouldn't take effect immediately
// on the underlying object, you need to call this.helper.CommitChanges() at which point the changes
// will be made to the datasource object.
// this.helper.CommitChanges();
Console.WriteLine(this.myObjectDataSource.Property1.ToString());
Console.WriteLine(this.myObjectDataSource.Property2.ToString());
Console.WriteLine(this.myObjectDataSource.Property3.ToString());
}
}
/// <summary>
/// My item.
/// </summary>
public class MyItem
{
/// <summary>
/// Gets or sets a value indicating whether this <see cref="MyItem"/> is property1.
/// </summary>
/// <value><c>true</c> if property1; otherwise, <c>false</c>.</value>
public bool Property1
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="MyItem"/> is property2.
/// </summary>
/// <value><c>true</c> if property2; otherwise, <c>false</c>.</value>
public bool Property2
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="MyItem"/> is property3.
/// </summary>
/// <value><c>true</c> if property3; otherwise, <c>false</c>.</value>
public bool Property3
{
get;
set;
}
}
/// <summary>
/// The checked list box property helper. This binds datasource properties to checkedlistbox items.
/// </summary>
public class CheckedListBoxPropertyHelper<T> where T : class
{
/// <summary>
/// The checked list box.
/// </summary>
private CheckedListBox checkedListBox;
/// <summary>
/// The property info.
/// </summary>
private PropertyInfo[] PropertyInfo;
/// <summary>
/// Initializes a new instance of the <see cref="CheckedListBoxPropertyHelper"/> class.
/// </summary>
/// <param name="checkedListBox">The checked list box.</param>
/// <param name="dataSource">The data source.</param>
/// <param name="bindImmediate">if set to <c>true</c> [bind immediate].</param>
public CheckedListBoxPropertyHelper(CheckedListBox checkedListBox, T dataSource, bool bindImmediate)
{
this.checkedListBox = checkedListBox;
this.DataSource = dataSource;
this.PropertyInfo = this.DataSource.GetType().GetProperties();
this.BindImmediate = bindImmediate;
this.checkedListBox.ItemCheck += new ItemCheckEventHandler(CheckedListBox_ItemCheck);
}
/// <summary>
/// The data source.
/// </summary>
public T DataSource
{
get;
private set;
}
/// <summary>
/// Gets or sets a value indicating whether to [bind immediately] to the datasource object.
/// </summary>
/// <value><c>true</c> if [bind immediately]; otherwise, <c>false</c>.</value>
public bool BindImmediate
{
get;
set;
}
/// <summary>
/// Commits the changes.
/// </summary>
public void CommitChanges()
{
if (!this.BindImmediate)
{
for (int i = 0; i < this.checkedListBox.Items.Count; i++)
{
CheckedPropertyItem checkedItem = this.checkedListBox.Items[i] as CheckedPropertyItem;
this.SetChecked(this.checkedListBox.GetItemChecked(i), checkedItem);
}
}
else
{
throw new InvalidOperationException("You cannot commit changes when bind immediate is on.");
}
}
/// <summary>
/// Adds the check list box item.
/// </summary>
/// <param name="item">The item.</param>
public void AddCheckListBoxItem(CheckedPropertyItem item)
{
PropertyInfo info = this.GetPropertyInfo(item.PropertyName);
bool isChecked = false;
if (info != null)
{
isChecked = (bool)info.GetValue(this.DataSource, null);
}
this.checkedListBox.Items.Add(item, isChecked);
}
/// <summary>
/// Handles the ItemCheck event of the CheckedListBox control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.ItemCheckEventArgs"/> instance containing the event data.</param>
private void CheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (BindImmediate)
{
CheckedListBox clb = sender as CheckedListBox;
CheckedPropertyItem checkedItem = clb.Items[e.Index] as CheckedPropertyItem;
this.SetChecked(
e.NewValue == CheckState.Checked ? true : false,
checkedItem);
}
}
/// <summary>
/// Sets the checked.
/// </summary>
/// <param name="isChecked">if set to <c>true</c> [is checked].</param>
/// <param name="clb">The CLB.</param>
private void SetChecked(bool isChecked, CheckedPropertyItem item)
{
PropertyInfo info = this.GetPropertyInfo(item.PropertyName);
if (info.CanWrite)
{
info.SetValue(this.DataSource, isChecked, null);
}
}
/// <summary>
/// Gets the property info.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <returns></returns>
private PropertyInfo GetPropertyInfo(string propertyName)
{
return this.PropertyInfo
.Where(c => c.Name == propertyName)
.SingleOrDefault();
}
}
/// <summary>
/// Checked Property Item binding.
/// </summary>
public class CheckedPropertyItem
{
/// <summary>
/// Initializes a new instance of the <see cref="CheckedPropertyItem"/> class.
/// </summary>
/// <param name="title">The title.</param>
/// <param name="propertyName">Name of the property.</param>
public CheckedPropertyItem(string title, string propertyName)
{
this.Title = title;
this.PropertyName = propertyName;
}
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>The title.</value>
public string Title
{
get;
private set;
}
/// <summary>
/// Gets the name of the property.
/// </summary>
public string PropertyName
{
get;
private set;
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
public override string ToString()
{
return this.Title;
}
}

Categories

Resources